# Identify, for each column of a matrix, numbers that are smaller than the previous ones, creating a 0 1 output having the same size of the original matrix

3 views (last 30 days)
Simone A. on 13 Jan 2023
Edited: Simone A. on 14 Jan 2023
Dear all,
I've got a matrix, let's call it M.
M = 1000x1200
The first 3 columns of M look like:
1.05 21.22 17.2 ...
2.01 22.35 18.2 ...
3.52 23.63 19.2 ...
4.32 23.54 20.2 ...
5.11 23.56 18.2 ...
6.89 23.64 19.2 ...
7.01 23.65 20.3 ...
8.87 23.99 21.3 ...
6.52 24.52 22.3 ...
7.91 24.51 23.3 ...
8.42 24.515 24.3 ...
9.03 24.525 22.3 ...
10.88 25.23 24.4 ...
11.01 45.12 25.4 ...
11.32 48.13 24.4 ...
11.33 46.14 26.4 ...
12.12 46.15 31.1 ...
12.09 48.142 32.2 ...
12.11 48.143 30.02 ...
12.13 48.151 31.6 ...
12.52 48.36 33.1 ...
Now, what I am trying to do is to loop through each column of M, from column 1 to column 1200, and identify all of the numbers that are smaller than the last, greater, number of a sequence. From this loop I'm trying to obtain a matrix, let's call it Mx of the same size of M, made of 0 and 1, where 1s denote the numbers that are smaller than the preavious ones.
For the example above, Mx would be:
0 0 0 ...
0 0 0 ...
0 0 0 ...
0 1 0 ...
0 1 1 ...
0 0 1 ...
0 0 1 ...
0 0 0 ...
1 0 0 ...
1 1 0 ...
1 1 0 ...
0 0 1 ...
0 0 0 ...
0 0 0 ...
0 0 1 ...
0 1 0 ...
0 1 0 ...
1 1 0 ...
1 0 1 ...
0 0 1 ...
0 0 1 ...
Any help would be massively appreciated!
##### 2 CommentsShow 1 older commentHide 1 older comment
Simone A. on 14 Jan 2023
Edited: Simone A. on 14 Jan 2023
* EDIT: Attached a sample of my dataset.*
Hi @Walter Roberson, it's me again. Altought the code runs brilliantly, I encountered a situation which i did not considered in my original question.
The previous code correctly identifies all of the values less than the maximum seen so far. The figure below shows colum 1 of my matrix with red circles indicating the values more than the minimum seen so far (namely the values identified with 1s):
However, I encountered a situation where values are not more than the minimum seen so far, but deviate from a linear trend, or better say "make some steps":
These are not considered in the script you kindly suggested.
I wonder if these deviations from the linear trend could also be identified as 1s?
I have included a sample of my actual dataset.
Thank a lot!

the cyclist on 13 Jan 2023
My output is a bit different from yours, but I think I followed the algorithm you wanted.
M = [ ...
1.05 21.22 17.2; ...
2.01 22.35 18.2; ...
3.52 23.63 19.2; ...
4.32 23.54 20.2; ...
5.11 23.56 18.2; ...
6.89 23.64 19.2; ...
7.01 23.65 20.3; ...
8.87 23.99 21.3; ...
6.52 24.52 22.3; ...
7.91 24.51 23.3; ...
8.42 24.515 24.3; ...
9.03 24.525 22.3; ...
10.88 25.23 24.4; ...
11.01 45.12 25.4; ...
11.32 48.13 24.4; ...
11.33 46.14 26.4; ...
12.12 46.15 31.1; ...
12.09 48.142 32.2; ...
12.11 48.143 30.02; ...
12.13 48.151 31.6; ...
12.52 48.36 33.1]; ...
nrows = height(M);
Mx = zeros(size(M));
for nr = 2:nrows
Mx(nr,:) = M(nr,:) < max(M(1:nr,:));
end
Mx
Mx = 21×3
0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 1 0 0 0 0 0 0 1 0 0 1 1 0
Simone A. on 14 Jan 2023
This is absolutelly perfect! Thanks a lot for your help!

### Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!