Size mismatching using "find" command

2 次查看(过去 30 天)
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by