i have this matrix (M)
M=[
1 NaN NaN 0.9 0.3;
2 14.1450 5.142 0.85 -0.1145;
3 NaN NaN NaN NaN;
4 NaN NaN 1.25 0.5;
5 18.45 9.3507 1.63 0.049;
6 NaN NaN NaN NaN;
7 17.16 0 0.7163 0.2791;
8 NaN NaN 1 0.35;
9 NaN NaN NaN NaN]
i want to reshape like this
P=[
1 1 17.16 0 0.7163 0.2791;
2 6 18.45 9.3507 1.63 0.049;
3 3 14.1450 5.142 0.85 -0.1145;
4 5 NaN NaN 1.25 0.5;
5 2 NaN NaN 0.9 0.3;
6 8 NaN NaN 1 0.35;
7 4 NaN NaN NaN NaN;
8 7 NaN NaN NaN NaN;
9 9 NaN NaN NaN NaN ]
NOTE: in first matrix 3rd coloum 7 row it has zero that why it came 1st
earlier it was 9x5 then it convert into 9x6
how can i make this?

6 Comments

You seem to be doing a sort of some kind, but I do not see what sorting rule you are using? You mention column 3 but your second row has a higher value for that column than the third row does? And I do not understand where that second column is coming from ??
in output 1st column indicate just an serial number
2nd column indicate previous data's serial number
Rule: in 3rd column "0" must be first others are randomly
then comes with all are filled up rows
then comes with filled data in 4th and 5th colunm
then NaN
But row must be same do not change the data of row, but just swap it up and down
increasing order is doesnt matter, but the data should not change, just swape as it is whole row,
you can set as in increasing order
2nd column indicate previous data's serial number
That does not appear to be the case. The previous serial number for the 18.45 row was 5, but it becomes 6 in the output. The previous serial number for the 17.16 line was 7, but it becomes 1 in the output.
P=[ 1 7 17.16 0 0.7163 0.2791;
2 2 14.1450 5.142 0.85 -0.1145;
3 5 18.45 9.3507 1.63 0.049;
4 1 NaN NaN 0.9 0.3;
5 8 NaN NaN 1 0.35;
6 4 NaN NaN 1.25 0.5;
7 3 NaN NaN NaN NaN;
8 6 NaN NaN NaN NaN;
9 9 NaN NaN NaN NaN ]
now this output data with no confusion
M= [1.0000 NaN NaN 0.9000 0.3000;
2.0000 14.1450 NaN 0.8500 -0.1145;
3.0000 NaN NaN NaN NaN;
4.0000 NaN NaN 1.2500 0.5000;
5.0000 18.4500 9.3507 1.6300 0.0490;
6.0000 NaN NaN NaN NaN;
7.0000 17.1600 0 0.7163 0.2791;
8.0000 13.1500 NaN 1.0000 NaN;
9.0000 NaN NaN NaN NaN]
[~,is]=sortrows([sum(isnan(M),2) any(M==0,2)],[1 -2])
P=M(is,:)
P=[(1:size(P,1))', P]
using this equations, i get this,
P =
1.0000 7.0000 17.1600 0 0.7163 0.2791
2.0000 5.0000 18.4500 9.3507 1.6300 0.0490
3.0000 2.0000 14.1450 NaN 0.8500 -0.1145
4.0000 1.0000 NaN NaN 0.9000 0.3000
5.0000 4.0000 NaN NaN 1.2500 0.5000
6.0000 8.0000 13.1500 NaN 1.0000 NaN
7.0000 3.0000 NaN NaN NaN NaN
8.0000 6.0000 NaN NaN NaN NaN
9.0000 9.0000 NaN NaN NaN NaN
but i expect this
P =
1.0000 7.0000 17.1600 0 0.7163 0.2791
2.0000 5.0000 18.4500 9.3507 1.6300 0.0490
3.0000 2.0000 14.1450 NaN 0.8500 -0.1145
4.0000 8.0000 13.1500 NaN 1.0000 NaN
5.0000 1.0000 NaN NaN 0.9000 0.3000
6.0000 4.0000 NaN NaN 1.2500 0.5000
7.0000 3.0000 NaN NaN NaN NaN
8.0000 6.0000 NaN NaN NaN NaN
9.0000 9.0000 NaN NaN NaN NaN
{ 1st priority is 4th column: data must be in sequence (0,data,...,data,NaN,....NaN) }
{ 2rnd priority is 3rd column: data must be in sequence (data,...,data,NaN,...,NaN) }
{ 3rd priority is 5th & 6th column: data must be in sequency(data,.....data,NaN,...NaN)}
{ 4th priority for all "NaN" terms in others row }
{ Data of row should not be change just swap the row with priority}
{ one more value is needed, in above matrix 3rd column hsve 4 data so i want 4 as output (A=4)}

Sign in to comment.

 Accepted Answer

M.Many
M.Many on 28 Nov 2020
Try this :
M= [1.0000 NaN NaN 0.9000 0.3000;
2.0000 14.1450 NaN 0.8500 -0.1145;
3.0000 NaN NaN NaN NaN;
4.0000 NaN NaN 1.2500 0.5000;
5.0000 18.4500 9.3507 1.6300 0.0490;
6.0000 NaN NaN NaN NaN;
7.0000 17.1600 0 0.7163 0.2791;
8.0000 13.1500 NaN 1.0000 NaN;
9.0000 NaN NaN NaN NaN]
[P,index] = sortrows(M,3)
P(isnan(P(:,3)),:) = sortrows(P(isnan(P(:,3)),:),2)
P(isnan(P(:,2)),:) = sortrows(P(isnan(P(:,2)),:),4)
P(isnan(P(:,4)),:) = sortrows(P(isnan(P(:,4)),:),5)
P = [ [1:9]' P]

More Answers (0)

Asked:

on 26 Nov 2020

Answered:

on 28 Nov 2020

Community Treasure Hunt

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

Start Hunting!