How to generate a new array from if statement embedded in for loop
20 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi MATLAB community,
I have a problem when I am trying to generate a new data array by processing it with an if statment embeded in a for loop.
Some backgroung: I have a cell array {181 x 1}, each of sub-cell arrays has a size 3059 x 5, double. Where 1st column is time point, 2nd cloumn is x_point, and column 5 is intensity.
I have created a separate array where I can set the intensity treshold. For instance it is column 5*0.8
I would like to generate a new data array that would contain only two columns x_point, and time_point that coreesponds to the time when value in the column 5 has droped bellow the intensity treshold. So this would be a new array with the size 3059 x 2.
I have tried to do that by embeding an if statement into for loop with the following code
Cellarray = accumarray(data(:,1),(1:size(data,1)),[],@(x){data(x,:)});
% Set the drop in intesity. If the intesity droped by 20% the intensity drop
% would be 0.2
Intensity_drop=0.2;
%Calculate intesity treashold for the first set of cells
Intensity_treshold=[Cellarray{1,1}(:,5)]*(1-Intensity_drop);
%number of pcells
numbpcells=numel(Intensity_treshold);
%number of time frames
time_frames=numel(Cellarray);
for k=1:time_frames
for i=1:numbpcells
if Cellarray{k,1}(i,5)<=Intensity_treshold(i)
N=[Cellarray{k,1}(i,2),k];
break
else
return
end
end
end
I was expecting it to run trough all the iterations until it gets to the value which is bellow treshold, and then diplay the data in a form of new array N. But this did not happen. Or at least Idid not get any N data as a return.
Could you sudgest what I did wrong, or is there another way to obtain the data set I am after.
I would be really greatfull,
2 commentaires
Réponses (1)
Reshma Nerella
le 24 Juil 2020
Hi,
I understand that you want to terminate the execution of both inner and outer loops once the condition is satisfied. It can be done using a variable.
status = 1; % initialising a variable
for k=1:time_frames
for i=1:numbpcells
if Cellarray{k,1}(i,5)<=Intensity_treshold(i)
N=[Cellarray{k,1}(i,2),k];
status = 0; % required condition is met
end
if status == 0
break % break inner for loop
end
end
if status == 0
break % break outer for loop
end
end
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!