IQ test, why does matlab see different answers?
1 次查看(过去 30 天)
显示 更早的评论
I have simple program which I will use later for more difficult, why do i get different answers for FIND and IF ------------------MATLAB code here-------------------------------------------
clear all, clc
b=0;
d=0;
b=[];
A=rand(5,5);
[r,c]=size(A);
inx=find(A>0.5);
disp(['There are ' num2str(length(inx)) ' no numbers more than 4'])
for i=1:r;
for j=i:c;
if A(i,j)>0.5;
d=d+1;
b=[b A(i,j)];
end;
end;
end;
if d>0;
disp(['There are ' num2str(d) ' numbers more than 4'])
else d==0;
disp(['There are no numbers more than 4'])
end;
---------------------End of MATLAB code-----------------------------------
1 个评论
Oleg Komarov
2012-8-6
You can format your code: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
采纳的回答
Kye Taylor
2012-8-6
Your inner loop should run from
j = 1:c
not
j = i:c
2 个评论
Sean de Wolski
2012-8-6
Please accept the answer that helped you resolve this in order to mark the thread closed.
更多回答(1 个)
Star Strider
2012-8-6
MATLAB doesn't see different answers, your code does. In your ‘for’ loop you're only seeing the upper triangular part of ‘A’. The ‘find’ function sees the entire ‘A’ matrix.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!