how to use abs function in this code?
Ältere Kommentare anzeigen
%Write a Matlab script to create random integer matrix with elements between -30 and 30.
%The size of the matrix must be first be read from user input. Use loops to sum of each row.
%Then find the differences between all pairs of these sums.
a=input('Enter the size of rows:');
b=input('Enter the size of columns:');
A=randi([-30 30],a,b)
r=zeros(a);
d=zeros(a);
for i=1:a
for j=1:b
r(i)=r(i)+A(i,j);
end
end
for k=1:a
for l=1:a
d(k,l)=(r(k)-r(l));
end
end
difference=triu(d,1)
%So I write this code and it worked -with a problem-. Let's say I created a 2x2 Matrix.
%TheSumOfFirstRow=2, TheSumOfSecondRow=3. As the result I wanna see both 2-3=-1 and 3-2=1.
%But the code that I wrote calculates just 2-3=-1.
Can someone please help :)
Antworten (1)
KALYAN ACHARJYA
am 26 Mai 2020
a=input('Enter the size of rows:');
b=input('Enter the size of columns:');
A=randi([-30 30],a,b)
r=zeros(a);
d=zeros(a);
for i=1:a
for j=1:b
r(i)=r(i)+A(i,j);
end
end
for k=1:a
for l=1:a
d(k,l)=abs((r(k)-r(l)));
end
end
difference=triu(d,1)
4 Kommentare
Ozan Mirzanli
am 26 Mai 2020
KALYAN ACHARJYA
am 26 Mai 2020
Then what actually you are trying to do?
Is this?
a=input('Enter the size of rows:');
b=input('Enter the size of columns:');
A=randi([-30 30],a,b)
sum_row=zeros(1,a);
for i=1:a
sum_row(i)=sum(A(i,:));
end
for k=1:length(sum_row)-1
d(k)=sum_row(k+1)-sum_row(k);
end
d(end)=sum_row(end)-sum_row(1);
Difference=d
Ozan Mirzanli
am 26 Mai 2020
Ozan Mirzanli
am 26 Mai 2020
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!