Suppose my matrix is A
and I sort the rows of this matrix. How can the same sequence be repeated on the matrix B
(the same course of course)?
Example
A = RAND (3,4); [Val indus] = sort (a, 2); B = RAND (3,4);
It is best that I came with
m = size (A). , 1); B = B (BSXFone (@ plus, (IND-1) * M, (1: M) '));
Out of curiosity, any options?
Update: Profile at 2008a (XP):
n = N
0.048524 1.4632 1.4791 1.195 1.0662 1.108 1.0082 0.96335 0.93155 0.90532 0.88976
n = 2m
0.63202 1.3029 1.1112 1.0501 0.94703 0.92847 0.90411 0.8849 0.8667 0.92098 0.85569
It just shows that the loops are probably no more thanks for the MATLAB programmer (probably).
A clear way to do this is to use a loop
A = RAND (3,4); B = RAND (3,4); [SortedA, ind] = Sort (A, 2); For R = 1: Size (A, 1) B (R, :) = B (R, Ind, R, :)); Finally, the loop version is fast for small (& lt; 12 rows) and large (> ~ 700 rows) class array (r2010a, OS X). The more columns are relative to the rows, the loop performance is better. Here is the code that I'm hacking quickly for testing:
siz = 10: 100: 1010; TT = zero (100,2, length (sis)); For s = siz k = 1: 100a = rand (s, 1 * s); B = RAND (S, 1 * S); [SortedA, ind] = Sort (A, 2); Tic; For R = 1: Size (A, 1) B (R, :) = B (R, Ind, R, :)); End, TT (K, 1, S == Siez) = Toss; Tic; I = size (A, 1); B = B (BSXFON (@ plus, (IND-1) * M, (1: M). ')); Tt (k, 2, s == siz) = toc; End of end M = Squeeze (Mean (TT, 1)); For square array
ans = 0.7149 2.1508 1.2203 1.4684 1.2339 1.1855 1.0212 1.0201 0.8770 0.8584m (1, :). 0.8445
Similar to the rows of two rows (number of rows)
Code> ans = 0.8431 1.2874 1.3550 1.1311 0.9 979 0.9 921 0.8263 0.7697 0.6856 0.7004 0.7314
No comments:
Post a Comment