How to select with several conditions in a matrix

2 Ansichten (letzte 30 Tage)
Luisa Andrade
Luisa Andrade am 23 Nov. 2021
Bearbeitet: Luisa Andrade am 25 Nov. 2021
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]';

Akzeptierte Antwort

Image Analyst
Image Analyst am 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 Kommentare
Image Analyst
Image Analyst am 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 am 24 Nov. 2021
Because of the second condition, "at least two consecutive times" ;)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by