Size mismatching using "find" command

1 visualizzazione (ultimi 30 giorni)
Matteo Migone
Matteo Migone il 12 Ago 2022
Commentato: Voss il 14 Ago 2022
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

Risposta accettata

Voss
Voss il 12 Ago 2022
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

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by