Loop to Identify changes/steps for each column of a matrix

1 次查看(过去 30 天)
Dear all,
I've got a matrix, let's call it M.
What I need to do, is to identify all of the values, for each column, that are both less than the maximum seen so far and follow a linear pattern.
Someone kindly helped me to write a code to address the first task (i.e., identify values less than the maximum seen so far), but I still need to meet the second condition, as I didn't realise that the second condition would have occurred in my dataset:
nrows = height(M);
Mx = zeros(size(M));
runningmax = M(1,:);
for nr = 2:nrows
oldrunningmax = runningmax;
runningmax = min(runningmax, M(nr, :));
Mx(nr,:) = M(nr,:) > runningmax | M(nr, :) == oldrunningmax;
end
Mx
Now, the code above correctly identifies all of the values less than the maximum seen so far (red circles in the figure below):
However, I also need to identify the values that deviate from a linear pattern, such as the major steps in the figure below:
I have attached a data sample (M), which is a portion of a column of my matrix (the one in the figure above).
Could you please help me to amend the code above to correctly meet both conditions?
Thanks a lot!
  2 个评论
Jan
Jan 2023-1-17
Do I understand correctly, that the part until "However, I also need" has no relation to the current question? Unrelated information reduce the chance to get a useful answer.
I assume the first part can be simplified using: M < cummax(M, 1) . No loop is required.
Simone A.
Simone A. 2023-1-17
编辑:Jan 2023-3-7
Hi Jan, thanks for your comment. That probably wasn't too clear, sorry about that. The first code is quasi exactly what I am after, but it should also flag the "steps" in the second figure. I've attached it to the question as it would be great if both conditions could be within the same code. This is the link to my original question, which I hope can clarify my point: https://uk.mathworks.com/matlabcentral/answers/1893760-identify-for-each-column-of-a-matrix-numbers-that-are-smaller-than-the-previous-ones-creating-a-0 If that's still not clear, please let me know and I'll try my best to make it clearer. Thanks!

请先登录,再进行评论。

采纳的回答

Vinayak Gupta
Vinayak Gupta 2023-3-7
Hi Simone,
I went through your last question and the file that you shared. The first problem is to identify values that are less than the current maximum in each column. As suggested by Jan, it can be efficiently calculated using cummax.
The example from your previous question:
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];
Mx = M < cummax(M,1)
Mx = 21×3 logical array
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 1 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0 1 0 0 1 0 1 0 0 1 0 0 0
The second problem is to identify points where data deviates from a linear pattern. This is a basic problem of second order derivatives. You can read about calculating derivatives here.
Mdotdot = diff(M,2);
As you might notice, there are a lot of concerned points, we might reduce them by adding some tolerance or using regressions. That will depend upon your problem statement.
I have calculated major points by keeping tolerance of 0.0001 for a small dataset.
sample = [46.4317555606203;
46.4293527462125;
46.4269518155469;
46.4245509077724;
46.4221499999979;
46.4197490922233;
46.4193745945352;
46.4190278418127;
46.4186810890903;
46.4183343363678;
46.4159619684783;
46.4135587379910;
46.4111555075037;
46.4087522770164;
46.4063485181607;
46.4039447504363;
46.4015409827118;
46.3991372149874;
46.3967358074631;
46.3943344432232;
46.3919330789833];
tol = 0.0001;
plot(abs(diff(sample,2)) > tol)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by