Size mismatching using "find" command

조회 수: 1(최근 30일)
Matteo Migone
Matteo Migone 2022년 8월 12일
댓글: Voss 2022년 8월 14일
Hi everyone,
I'm experiencing some issues using the find command inside a for loop. I have a 28x230 matrix (mean_GMT_min_mean_ref); of every row, I want to detect the first time the value 2, when present, is reached. That is the problem. Given this code
for i=1:length(FileList)
y(i,1) = find((floor(mean_GMT_min_mean_ref(i,:))==2),1,'first');
value_2(i,1) = mean_GMT_min_mean_ref(i,y(i));
year_gwl_2(i,1) = x(y(i,1));
end
I get the error "Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-0." This because e.g. the second line of the matrix (and others) is made of values all < 2. I would just like, in the case that value isn't reached, to get a NaN in the column vector (y, value_2 and year_gwl_2 are all 28x1 vectors), instead of quitting the for loop with that annoying error.
Can anyone help me with this? Thanks in advance, Matteo

채택된 답변

Voss
Voss 2022년 8월 12일
for i=1:length(FileList)
idx = find((floor(mean_GMT_min_mean_ref(i,:))==2),1,'first');
if isempty(idx)
y(i,1) = NaN;
value_2(i,1) = NaN;
year_gwl_2(i,1) = NaN;
else
y(i,1) = idx;
value_2(i,1) = mean_GMT_min_mean_ref(i,y(i));
year_gwl_2(i,1) = x(y(i,1));
end
end
  댓글 수: 2
Voss
Voss 2022년 8월 14일
You're welcome!

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

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by