Hi,
I want to compare each element of matrix with 'total 'and check if it is equal.
Code gives error. How can I do that please?
clc
clear
b=[2 2]
t=[1 1 ; 1 1]
total=2;
for v=1:b(1)
rowsum(v,1)=sum(t(v,:));
end
for y=1:b(2)
colsum(v,1)=sum(t(:,v));
end
if rowsum(:,1)==total && colsum(:,1)==total
disp 'yes'
end

 Akzeptierte Antwort

madhan ravi
madhan ravi am 3 Apr. 2019

1 Stimme

No loops needed:
if all(sum(t)==total)
disp('Yes')
end
if all(sum(t,2)==total)
disp('Yes')
end

3 Kommentare

Hasan Kayhan Kayadelen
Hasan Kayhan Kayadelen am 3 Apr. 2019
Thank you, what is the purpose of using 'all' ? If I am not mistaken, it also works without 'all'.
madhan ravi
madhan ravi am 3 Apr. 2019
Bearbeitet: madhan ravi am 3 Apr. 2019
all() is appropriate here because we check that each row or column sum equals to total. https://in.mathworks.com/help/matlab/ref/all.html?searchHighlight=All&s_tid=doc_srchtitle - read it
Hasan Kayhan Kayadelen
Hasan Kayhan Kayadelen am 3 Apr. 2019
Bearbeitet: Hasan Kayhan Kayadelen am 3 Apr. 2019
Thank you so much, I learned new things, all() and any() will be very useful.
Regards,

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by