필터 지우기
필터 지우기

IQ test, why does matlab see different answers?

조회 수: 1 (최근 30일)
Algirdas Kluonius
Algirdas Kluonius 2012년 8월 6일
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-----------------------------------

채택된 답변

Kye Taylor
Kye Taylor 2012년 8월 6일
Your inner loop should run from
j = 1:c
not
j = i:c
  댓글 수: 2
Algirdas Kluonius
Algirdas Kluonius 2012년 8월 6일
thanks very much, i didn't see that it was i , it solved everything :)
Sean de Wolski
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
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.
  댓글 수: 1
Algirdas Kluonius
Algirdas Kluonius 2012년 8월 6일
u are also right , because of i instead of 1 it was a fcn for a upper triangle :) thx again

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by