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

 채택된 답변

madhan ravi
madhan ravi 2019년 4월 3일

1 개 추천

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

댓글 수: 3

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by