Prev: Adapt function problem
Next: Help on implementing motor torque control reg with already available inefficient motor
From: Thomas on 2 Jul 2010 05:38 How can thsi be vectorized whe lamda has a size of nx3 [l,ind] = sort(lamda,2); for i=1:size(lamda,1) lamda2(i,:) = lamda(i,ind(i,:)); end Thanks thomas
From: Oleg Komarov on 2 Jul 2010 05:47 "Thomas " <steinweger(a)tu-harburg.de> wrote in message <i0kc1v$628$1(a)fred.mathworks.com>... > How can thsi be vectorized whe lamda has a size of nx3 > > [l,ind] = sort(lamda,2); > for i=1:size(lamda,1) > lamda2(i,:) = lamda(i,ind(i,:)); > end > > Thanks thomas isequal(l, lambda2) % 1 Oleg
From: Thomas on 2 Jul 2010 07:30 "Oleg Komarov" <oleg.komarovRemove.this(a)hotmail.it> wrote in message <i0kcin$9k0$1(a)fred.mathworks.com>... > "Thomas " <steinweger(a)tu-harburg.de> wrote in message <i0kc1v$628$1(a)fred.mathworks.com>... > > How can thsi be vectorized whe lamda has a size of nx3 > > > > [l,ind] = sort(lamda,2); > > for i=1:size(lamda,1) > > lamda2(i,:) = lamda(i,ind(i,:)); > > end > > > > Thanks thomas > > isequal(l, lambda2) % 1 > > Oleg Indeed, l and lamda are equal. But this doesn't matter to my question! If you like treat this problem: lamda = rand(10,3)-0.5; [l,ind] = sort(abs(lamda),2); for i=1:size(lamda,1) lamda2(i,:) = lamda(i,ind(i,:)); end Thomas
From: Bruno Luong on 2 Jul 2010 08:01 "Thomas " <steinweger(a)tu-harburg.de> wrote in message <i0kike$ajp$1(a)fred.mathworks.com>... > for i=1:size(lamda,1) > lamda2(i,:) = lamda(i,ind(i,:)); > end The for-loop can be replaced by: m= size(lamda,1); lamda2 = lamda(bsxfun(@plus,(ind-1)*m,(1:m)')) % Bruno
From: Jos (10584) on 2 Jul 2010 08:13
"Thomas " <steinweger(a)tu-harburg.de> wrote in message <i0kike$ajp$1(a)fred.mathworks.com>... > "Oleg Komarov" <oleg.komarovRemove.this(a)hotmail.it> wrote in message <i0kcin$9k0$1(a)fred.mathworks.com>... > > "Thomas " <steinweger(a)tu-harburg.de> wrote in message <i0kc1v$628$1(a)fred.mathworks.com>... > > > How can thsi be vectorized whe lamda has a size of nx3 > > > > > > [l,ind] = sort(lamda,2); > > > for i=1:size(lamda,1) > > > lamda2(i,:) = lamda(i,ind(i,:)); > > > end > > > > > > Thanks thomas > > > > isequal(l, lambda2) % 1 > > > > Oleg > > Indeed, l and lamda are equal. But this doesn't matter to my question! > If you like treat this problem: > > lamda = rand(10,3)-0.5; > [l,ind] = sort(abs(lamda),2); > for i=1:size(lamda,1) > lamda2(i,:) = lamda(i,ind(i,:)); > end > > Thomas Oleg gave you a valid question to your original question, so you really should write "but this doesn't matter to my OTHER question". Anywats, you might be interested in my (vectorized) function SORTLIND, available on the FEX: http://www.mathworks.com/matlabcentral/fileexchange/19443 lamda = rand(10,3)-0.5; [unused, ind] = sortlind(abs(lamda),2); lamda2 = lamda(ind) Alternatively, you could pre-allocate lamda2 making the for-loop pretty fast as well: lamda2 = zeros(size(lamda)) ; for i=1:size(lamda,1) lamda2(i,:) = lamda(i,ind(i,:)); end hth Jos |