Prev: Adapt function problem
Next: Help on implementing motor torque control reg with already available inefficient motor
From: Matt Fig on 2 Jul 2010 10:14 How about this one: % Data lamda = rand(100,3)-.5 [l,ind] = sort(abs(lamda),2); % Engine lamda3 = sign(lamda); lamda3 = l .* lamda3(cumsum(ones(size(lamda3)))+(ind-1)*size(lamda3,1));
From: Bruno Luong on 2 Jul 2010 10:32 > % Engine > lamda3 = sign(lamda); > lamda3 = l .* lamda3(cumsum(ones(size(lamda3)))+(ind-1)*size(lamda3,1)); It seems the same work (on the sign array) can be done to extract the value directly, so I wonder what is the advantage of the above. lamda4 = lamda(cumsum(ones(size(lamda)))+(ind-1)*size(lamda,1)) Bruno
From: Matt J on 2 Jul 2010 12:16 "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <i0kroc$cb2$1(a)fred.mathworks.com>... > > If lamda is only nx3, I would actually expect a for-loop over columns, as opposed to rows, to be the most efficient approach, faster even than anything vectorized: > > > > for i=1:size(lamda,2) > > > > lamda2(:,i) = lamda(ind(:,i),i); > > > > end > > Nice try, but I believe that's won't work. Hmmm. I guess not. Oh well, a double for-loop might still be pretty fast, as long as we're talking about 3 columns, lamda2=lamda; for i=1:3 for j=1:3 idx= (ind(:,i)==j); lamda2(idx,i) = lamda(idx,j) ; end end
From: Matt Fig on 2 Jul 2010 13:07 "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <i0kt94$p8b$1(a)fred.mathworks.com>... > > > % Engine > > lamda3 = sign(lamda); > > lamda3 = l .* lamda3(cumsum(ones(size(lamda3)))+(ind-1)*size(lamda3,1)); > > It seems the same work (on the sign array) can be done to extract the value directly, so I wonder what is the advantage of the above. > > lamda4 = lamda(cumsum(ones(size(lamda)))+(ind-1)*size(lamda,1)) > > Bruno Good point! I guess that is what 5 hours of sleep does to a person....
From: Thomas on 5 Jul 2010 09:23
> 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 > Here is another short vectorized solution lamdaabs = abs(lamda); lamdamax = max(lamdaabs ,[],2); [m,loc] = ismember(lamdamax,lamdaabs); lamda2 = lamda(loc); Thomas Thanks to all who engage this problem! |