How to select with several conditions in a matrix

2 visualizzazioni (ultimi 30 giorni)
I want to select the data from falling below -1 until it turns positive or crosses zero, at least two consecutive times.
An example to clarify what I mean:
In the following matrix I want to selec the bold numbers:
b = [-0.32 -0.5 -1.1 -1.9 -0.5 -0.1 0.2 NaN -1.20 NaN NaN; -0.32 -0.5 -1.1 -1.9 -0.5 -0.1 0.2 NaN -1.20 -0.9 NaN]';

Risposta accettata

Image Analyst
Image Analyst il 23 Nov 2021
See if this is what you want:
b = [-0.32 -0.5 -1.1 -1.9 -0.5 -0.1 0.2 NaN -1.20 NaN NaN; -0.32 -0.5 -1.1 -1.9 -0.5 -0.1 0.2 NaN -1.20 -0.9 NaN]'
[rows, columns] = size(b)
bb = [0 0 0 0 0 0 0 0 0 0 0; 0 0 1 1 0 0 0 0 1 0 0]'
for col = 1 : columns
% Find out where this column is less than -1.
mask = b(:, col) < -1
% Find out the indexes where the runs below -1 start at.
startingIndexes = strfind(mask', [0, 1]) + 1
% Find out where this column is less than 0.
mask2 = b(:, col) < 0
for k = 1 : length(startingIndexes)
index1 = startingIndexes(k);
for row = index1 : rows
if mask2(row)
index2 = row;
else
break; % Went 0 or above so don't increment the second index.
end
end
% Set thos values of bb.
bb(index1:index2, col) = 1;
end
end
bb
  4 Commenti
Image Analyst
Image Analyst il 24 Nov 2021
Thanks for accepting. Though I was wondering why the last item, where the value is -1.2, you did not want to mark that location as 1. Is it because it did not go positive before it encountered a NaN?
Luisa Andrade
Luisa Andrade il 24 Nov 2021
Because of the second condition, "at least two consecutive times" ;)

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Prodotti


Release

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by