i m trying interpret this simple matrix code..

1 visualización (últimos 30 días)
Ann Lee
Ann Lee el 30 de Abr. de 2022
Comentada: Ann Lee el 1 de Mayo de 2022
seed:345
10x7 matrix,
[-150,300]
B=randi...
and i want to sort 10 elements of row3 by ascending order.
i m wondering >>> %parts!
>> rand('seed',345)
>> B=randi([-150,300],10,7)
B =
-148 86 -67 36 -15 -141 195
-38 177 41 124 266 74 284
27 95 32 107 45 204 206
76 182 11 269 -78 -80 -100
-34 275 -138 238 -48 20 15
171 -119 88 108 -79 195 -118
19 134 12 76 -35 -131 138
285 228 -78 148 -9 -107 -55
225 23 147 20 -73 155 237
-18 247 279 17 277 222 71
>> [y,in]=sort(B(:,3)) %using [y,in] this two variables because this is matrix with line,row
%so i m usinng two variables right?
%and i can change 'y','in' to another chracters right?
y =
-138
-78
-67
11
12
32
41
88
147
279
in =
5
8
1
4
7
3
2
6
9
10
>> B(in(1:end),:)
ans =
-34 275 -138 238 -48 20 15
285 228 -78 148 -9 -107 -55
-148 86 -67 36 -15 -141 195
76 182 11 269 -78 -80 -100
19 134 12 76 -35 -131 138
27 95 32 107 45 204 206
-38 177 41 124 266 74 284
171 -119 88 108 -79 195 -118
225 23 147 20 -73 155 237
-18 247 279 17 277 222 71

Respuesta aceptada

Voss
Voss el 30 de Abr. de 2022
rand('seed',345)
B=randi([-150,300],10,7)
B = 10×7
280 -19 68 -82 48 124 -130 240 129 -112 166 15 295 -20 249 300 200 -65 232 266 298 206 144 170 236 -78 150 -26 128 -19 166 86 235 48 -51 159 298 78 117 300 66 -2 -146 247 221 34 50 -102 -143 273 18 41 109 30 222 73 26 -65 -70 -149 89 49 242 121 139 -40 -97 286 120 161
[sorted_B_column_3,old_indices_in_B_column_3]=sort(B(:,3))
sorted_B_column_3 = 10×1
-112 -70 -40 41 68 78 166 170 200 221
old_indices_in_B_column_3 = 10×1
2 9 10 8 1 6 5 4 3 7
%using [y,in] this two variables because this is matrix with line,row
%so i m usinng two variables right?
% it's two variables because that's two outputs from the sort() function:
% (1) the sorted values, and (2) the index where each value was in the
% original (unsorted) array
%
% for instance, in this case:
% old_indices_in_B_column_3(1) is 2, which tells you that element #2 (-112) in column 3 of B is the smallest
% old_indices_in_B_column_3(2) is 9, which tells you that element #9 (-70) in column 3 of B is the next smallest
% etc.
%and i can change 'y','in' to another chracters right?
% yes, you can use any valid variable name, or use ~ if you don't need that
% output, e.g., [~,idx] = sort(B(:,3));

Más respuestas (1)

Riccardo Scorretti
Riccardo Scorretti el 30 de Abr. de 2022
rand('seed',345);
B = randi([-150,300], 10, 7);
[y,in] = sort(B(:,3));
You are sorting a column vector (= the 3rd column of B). The output are:
  • y = sorted vector
  • in = index such that y = B(in,3)
y'
ans = 1×10
-112 -70 -40 41 68 78 166 170 200 221
B(in,3)'
ans = 1×10
-112 -70 -40 41 68 78 166 170 200 221
It doen't matter whether you are sorting a column vector, a matrix of whatever: ask for two output arguments if you need to know the index (sometimes it is an important information).

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by