How to make a permanent change to a matrix using sortrows?

2 views (last 30 days)
The code im writing, im using sort rows and its working great, it takes my array of random numbers and then puts it in order completely but it isnt a permanent change to the array. From what I can tell the last part of my code prints out the array,sorts it and then prints it again but the array goes back to normal? is there anyway to keep sortrow changes to my array?
a=0; b=0; c=0; d=0; e=0; f=0;
k = 80; u = 40; sigmas = .05; %GPa
c1 = (k + (4/3)*u) ; c2 = (k - (2/3)*u);
P = zeros(50,7);
N = zeros(10,6);
for i = 1:50
for j = 1:6
P(i,j) = (-.0025 + (.005*rand(1)));
end
end
%for di = 1:10
for i = 1:50
a = P(i,1); d = P(i,2); f = P(i,3); b = P(i,4); c = P(i,5); e = P(i,6);
sigmaxx = (a*c1 + a*c2 + a*c2); sigmayy = (d*c2 + d*c1 + d*c2);
sigmazz = (f*c2+f*c2+f*c1); sigmaxy = (2*b*u);
sigmazx = (2*c*u); sigmayz = (2*e*u);
F = ((sigmaxx - sigmayy)^2)+((sigmayy-sigmazz)^2)+((sigmaxx-sigmazz)^2)+6*((sigmaxy^2)+(sigmazx^2)+(sigmayz^2));
Cf = ((F - (2*sigmas^2))/(2*sigmas^2))^2;
P(i,7) = Cf;
end
disp(P)
sortrows(P,7)
disp(P)
>> GE
1.0e+05 *
0.0000 0.0000 -0.0000 0.0000 0.0000 0.0000 0.1659
0.0000 0.0000 -0.0000 0.0000 0.0000 -0.0000 0.3517
-0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.0000 0.1382
0.0000 0.0000 0.0000 -0.0000 0.0000 0.0000 0.0815
-0.0000 0.0000 -0.0000 0.0000 0.0000 -0.0000 0.4886
0.0000 -0.0000 0.0000 0.0000 -0.0000 -0.0000 1.7163
0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.1106
0.0000 0.0000 0.0000 -0.0000 0.0000 0.0000 0.1178
0.0000 0.0000 -0.0000 -0.0000 0.0000 -0.0000 1.4394
-0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0000 0.3773
-0.0000 -0.0000 -0.0000 -0.0000 0.0000 -0.0000 0.0505
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.1971
-0.0000 0.0000 0.0000 -0.0000 0.0000 0.0000 1.2847
0.0000 0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0548
-0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.0000 0.7923
-0.0000 -0.0000 0.0000 0.0000 -0.0000 0.0000 1.5805
0.0000 -0.0000 -0.0000 0.0000 0.0000 -0.0000 0.6276
0.0000 -0.0000 -0.0000 0.0000 -0.0000 0.0000 1.1048
0.0000 0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0482
-0.0000 0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.4118
0.0000 0.0000 -0.0000 -0.0000 0.0000 -0.0000 0.2474
-0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.0000 0.5147
-0.0000 -0.0000 0.0000 0.0000 -0.0000 0.0000 0.7107
0.0000 0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0695
0.0000 0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.1451
0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0000 0.8630
0.0000 -0.0000 0.0000 -0.0000 0.0000 0.0000 0.1166
-0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.1223
-0.0000 -0.0000 0.0000 0.0000 0.0000 -0.0000 0.8756
-0.0000 -0.0000 0.0000 0.0000 -0.0000 -0.0000 0.3255
0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.8802
-0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0000 1.6178
-0.0000 -0.0000 0.0000 -0.0000 -0.0000 -0.0000 1.8305
0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.5213
-0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0000 2.2296
-0.0000 0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.3583
0.0000 0.0000 0.0000 -0.0000 0.0000 -0.0000 0.1360
-0.0000 -0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.0235
-0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.5958
-0.0000 -0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.2636
0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0000 1.1244
0.0000 -0.0000 -0.0000 0.0000 0.0000 0.0000 2.1475
-0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.3680
0.0000 -0.0000 0.0000 -0.0000 -0.0000 -0.0000 0.0437
-0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0000 0.9815
-0.0000 -0.0000 0.0000 0.0000 0.0000 -0.0000 0.2089
0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.0000 0.1444
0.0000 -0.0000 0.0000 0.0000 0.0000 0.0000 1.5641
-0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0000 0.2793
-0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 0.2917
ans =
1.0e+05 *
-0.0000 -0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.0235
0.0000 -0.0000 0.0000 -0.0000 -0.0000 -0.0000 0.0437
0.0000 0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0482
-0.0000 -0.0000 -0.0000 -0.0000 0.0000 -0.0000 0.0505
0.0000 0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0548
0.0000 0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0695
0.0000 0.0000 0.0000 -0.0000 0.0000 0.0000 0.0815
0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.1106
0.0000 -0.0000 0.0000 -0.0000 0.0000 0.0000 0.1166
0.0000 0.0000 0.0000 -0.0000 0.0000 0.0000 0.1178
-0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.1223
0.0000 0.0000 0.0000 -0.0000 0.0000 -0.0000 0.1360
-0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.0000 0.1382
0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.0000 0.1444
0.0000 0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.1451
0.0000 0.0000 -0.0000 0.0000 0.0000 0.0000 0.1659
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.1971
-0.0000 -0.0000 0.0000 0.0000 0.0000 -0.0000 0.2089
0.0000 0.0000 -0.0000 -0.0000 0.0000 -0.0000 0.2474
-0.0000 -0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.2636
-0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0000 0.2793
-0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 0.2917
-0.0000 -0.0000 0.0000 0.0000 -0.0000 -0.0000 0.3255
0.0000 0.0000 -0.0000 0.0000 0.0000 -0.0000 0.3517
-0.0000 0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.3583
-0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.3680
-0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0000 0.3773
-0.0000 0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.4118
-0.0000 0.0000 -0.0000 0.0000 0.0000 -0.0000 0.4886
-0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.0000 0.5147
0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.5213
-0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.5958
0.0000 -0.0000 -0.0000 0.0000 0.0000 -0.0000 0.6276
-0.0000 -0.0000 0.0000 0.0000 -0.0000 0.0000 0.7107
-0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.0000 0.7923
0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0000 0.8630
-0.0000 -0.0000 0.0000 0.0000 0.0000 -0.0000 0.8756
0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.8802
-0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0000 0.9815
0.0000 -0.0000 -0.0000 0.0000 -0.0000 0.0000 1.1048
0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0000 1.1244
-0.0000 0.0000 0.0000 -0.0000 0.0000 0.0000 1.2847
0.0000 0.0000 -0.0000 -0.0000 0.0000 -0.0000 1.4394
0.0000 -0.0000 0.0000 0.0000 0.0000 0.0000 1.5641
-0.0000 -0.0000 0.0000 0.0000 -0.0000 0.0000 1.5805
-0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0000 1.6178
0.0000 -0.0000 0.0000 0.0000 -0.0000 -0.0000 1.7163
-0.0000 -0.0000 0.0000 -0.0000 -0.0000 -0.0000 1.8305
0.0000 -0.0000 -0.0000 0.0000 0.0000 0.0000 2.1475
-0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0000 2.2296
1.0e+05 *
0.0000 0.0000 -0.0000 0.0000 0.0000 0.0000 0.1659
0.0000 0.0000 -0.0000 0.0000 0.0000 -0.0000 0.3517
-0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.0000 0.1382
0.0000 0.0000 0.0000 -0.0000 0.0000 0.0000 0.0815
-0.0000 0.0000 -0.0000 0.0000 0.0000 -0.0000 0.4886
0.0000 -0.0000 0.0000 0.0000 -0.0000 -0.0000 1.7163
0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.1106
0.0000 0.0000 0.0000 -0.0000 0.0000 0.0000 0.1178
0.0000 0.0000 -0.0000 -0.0000 0.0000 -0.0000 1.4394
-0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0000 0.3773
-0.0000 -0.0000 -0.0000 -0.0000 0.0000 -0.0000 0.0505
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.1971
-0.0000 0.0000 0.0000 -0.0000 0.0000 0.0000 1.2847
0.0000 0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0548
-0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.0000 0.7923
-0.0000 -0.0000 0.0000 0.0000 -0.0000 0.0000 1.5805
0.0000 -0.0000 -0.0000 0.0000 0.0000 -0.0000 0.6276
0.0000 -0.0000 -0.0000 0.0000 -0.0000 0.0000 1.1048
0.0000 0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0482
-0.0000 0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.4118
0.0000 0.0000 -0.0000 -0.0000 0.0000 -0.0000 0.2474
-0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.0000 0.5147
-0.0000 -0.0000 0.0000 0.0000 -0.0000 0.0000 0.7107
0.0000 0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0695
0.0000 0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.1451
0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0000 0.8630
0.0000 -0.0000 0.0000 -0.0000 0.0000 0.0000 0.1166
-0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.1223
-0.0000 -0.0000 0.0000 0.0000 0.0000 -0.0000 0.8756
-0.0000 -0.0000 0.0000 0.0000 -0.0000 -0.0000 0.3255
0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.8802
-0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0000 1.6178
-0.0000 -0.0000 0.0000 -0.0000 -0.0000 -0.0000 1.8305
0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.5213
-0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0000 2.2296
-0.0000 0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.3583
0.0000 0.0000 0.0000 -0.0000 0.0000 -0.0000 0.1360
-0.0000 -0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.0235
-0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.5958
-0.0000 -0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.2636
0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0000 1.1244
0.0000 -0.0000 -0.0000 0.0000 0.0000 0.0000 2.1475
-0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.3680
0.0000 -0.0000 0.0000 -0.0000 -0.0000 -0.0000 0.0437
-0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0000 0.9815
-0.0000 -0.0000 0.0000 0.0000 0.0000 -0.0000 0.2089
0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.0000 0.1444
0.0000 -0.0000 0.0000 0.0000 0.0000 0.0000 1.5641
-0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0000 0.2793
-0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 0.2917

Accepted Answer

Cedric
Cedric on 16 Oct 2017
Edited: Cedric on 16 Oct 2017
P = sortrows(P,7) ;
Most of the time functions don't operate "externally", so SORTROWS doesn't alter your array P defined in the workspace. It creates a new sorted array that it outputs. You have to store this output back into P if you want P to be the sorted version. If you wanted to keep the original, you could store it into a new variable:
P_sorted = sortrows(P,7) ;
  4 Comments
Shawn Blancett
Shawn Blancett on 17 Oct 2017
Hey if you dont mind me asking, is there a way to run a script that produces a plot more than once , in the end plotting 10 lines on the same graph?
Cedric
Cedric on 17 Oct 2017
Edited: Cedric on 17 Oct 2017
Yes, execute
hold on
before or after the first plot, and the following will be added as overlays.

Sign in to comment.

More Answers (0)

Categories

Find more on Linear Algebra 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!