Using Find() in For Loop

조회 수: 3 (최근 30일)
DYLAN
DYLAN 2012년 11월 30일
I am setting up a very simple matlab code to find the first value in each column of a matrix that passes a threshold value. If the matrix has a value that meets the threshold range, then it outputs an index. If the condition is not met, 'NaN' appears.The expected outcome is 1-by-5 matrix that has indice for each column.
However, I could not get IF statement to work. Each line of the code works fine but when it is put together, it does not produce the values I was expecting.
What am I missing here?
xn=[1 0 16 14 18;0 13 4 0 11; 0 4 15 1 52; 0 5 5 17 43; 0 11 5 3 14];
y=[1 24 35 15 16]; %Threshold
t=4*ones(2,5);
SD=1.5;
y_minuse_SD=y-SD*ones(1,5);
y_plus_SD=y+SD*ones(1,5);
d=1:length(xn);
for s=1:5;
if xn(d,s)<y_plus_SD(1,s) & xn(d,s)>=y_minuse_SD(1,s)
t(1,s)=find(xn(d,s)>=y_minuse_SD(1,s),1);
else
t(1,s)=NaN;
end
end
  댓글 수: 1
Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 30일
What is the expected value?

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

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 30일
xn=[1 0 16 14 18;0 13 4 0 11; 0 4 15 1 52; 0 5 5 17 43; 0 11 5 3 14];
y=[1 24 35 15 16]; %Threshold
[n,m]=size(xn);
SD=1.5;
y_m=y-SD;
y_p=y+SD;
z=xn(:);
out=arrayfun(@(x) find(z<y_p(x) & z>y_m(x),1),1:n,'un',0)
out(cellfun(@isempty,out))=num2cell(nan)

카테고리

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