Add value in a matrix
2 views (last 30 days)
Show older comments
Alejandro Fernández
on 20 Feb 2020
Commented: Alejandro Fernández
on 20 Feb 2020
Hi, I need to add a row of values to an array, taking into account the last row of my array (once sorted) I want the next row to show a number starting at 1 that is related to the previous row. Here is an example to try to explain it better
% First matrix
Z = [255 1 0 255 255 255 1
30 51 51 30 29 29 21
49 51 52 48 51 52 48
-5 4 4 -5 -4 -4 4];
% Orderly matrix in the 4th row
ZORD = [255 255 255 255 1 0 1
30 30 29 29 21 21 21
49 48 51 52 51 52 48
-5 -5 -4 -4 4 4 4];
% So far I know how to do it, but this is what I put in now is what
% I only know how to do with a for-loop but for my data size it is not very operational.
ZFIN = [255,255,255,255,1,0,1;
30,30,29,29,21,21,21;
49,48,51,52,51,52,48;
-5,-5,-4,-4,4,4,4;
1 1 2 2 3 3 3]
If someone knows how to do the last step I would be very grateful, thank you very much
0 Comments
Accepted Answer
JESUS DAVID ARIZA ROYETH
on 20 Feb 2020
ZORD=sortrows(Z',4)';
[~,~,C]=unique(ZORD(end,:));
ZFIN=[ZORD; C']
More Answers (0)
See Also
Categories
Find more on Shifting and Sorting Matrices 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!