How to select with several conditions in a matrix

2 次查看(过去 30 天)
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]';

采纳的回答

Image Analyst
Image Analyst 2021-11-23
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 个评论
Image Analyst
Image Analyst 2021-11-24
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 2021-11-24
Because of the second condition, "at least two consecutive times" ;)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by