How to select with several conditions in a matrix

3 visualizaciones (últimos 30 días)
Luisa Andrade
Luisa Andrade el 23 de Nov. de 2021
Editada: Luisa Andrade el 25 de Nov. de 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]';

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Productos


Versión

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by