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

 Réponse acceptée

madhan ravi
madhan ravi le 3 Avr 2019

1 vote

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

3 commentaires

Hasan Kayhan Kayadelen
Hasan Kayhan Kayadelen le 3 Avr 2019
Thank you, what is the purpose of using 'all' ? If I am not mistaken, it also works without 'all'.
madhan ravi
madhan ravi le 3 Avr 2019
Modifié(e) : madhan ravi le 3 Avr 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 le 3 Avr 2019
Modifié(e) : Hasan Kayhan Kayadelen le 3 Avr 2019
Thank you so much, I learned new things, all() and any() will be very useful.
Regards,

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by