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
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!