Matlab: sort by row

Why is this

t = magic(4);
k = 1:4;
tt(k,:) = sort(t(k,:)) % 

sort the values ​​inside each column (row swapping and cols does nothing), but it

t = magic(4);
for k = 1:4
  tt(k,:) = sort(t(k,:))
end

sorts the values ​​inside the string as expected?

+4
source share
2 answers

Next

t = magic(4);
k = 1:4;
tt(k,:) = sort(t(k,:)) % 

t(k,:)is a 4x4 matrix. Therefore, it sortwill apply its standard 1-dimensional sort, i.e. Wrt lines. Note that you can specify sortto sort by the second dimension. those. wrt columns

tt(k,:) = sort(t(k,:),2)

In another case k- an integer, and t(k,:)- a vector of a line 1x4; therefore, sorting will be done by wrt columns.

t = magic(4);
for k = 1:4
  tt(k,:) = sort(t(k,:))
end

, @Luis Mendo: , , :

: sort ( Matlab) , - . , .

+10

" , ?" . MATLAB . , MATLAB . , sum, max, min .. 2D-, , . , , , .

sum(M,2)

, sum(M) ( sum(M,1), ) .

dfri answer , 2D- t, t . - 1Dvector, , , .

+5

All Articles