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)
Show older comments
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 Comments
Answers (1)
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
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!