help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: remove for-loop from following code


From: Juan Pablo Carbajal
Subject: Re: remove for-loop from following code
Date: Sun, 6 Nov 2011 10:29:30 +0100

On Sun, Nov 6, 2011 at 9:52 AM, Stefaan Himpe <address@hidden> wrote:
>
>>> function [Ytr] = ytransform(D, Y)
>>>  [rows, cols] = size(Y);
>>>  for row = 1:rows
>>>    Ytr(:,row) = (1:D == Y(row));
>>>  endfor
>>> endfunction
>>>
>>> Is there a way to do implement this without for loop over all rows of Y?
>
>>
>> Try,
>> N = lenth(Y);
>> subs = [(1:N).' Y(:)];
>> Ytr = accumarray (subs, ones(N,1));
>
> Hello,
>
> Thanks for your reply.
> After scanning the octave documentation once more I finally arrived at the
> following solution (which uses some special provisions to create permutation
> matrices):
>
> function [Ytr] = ytransformed(K,Y)
>  Ytr = eye(K)(Y',:);
> endfunction
>
> Thanks for your reply!
> Best regards,
> Stefaan.
>
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://mailman.cae.wisc.edu/listinfo/help-octave
>
Nice solution.
Now, isn't K == max(Y)? You may reduce the arity of your funcion, if so.


-- 
M. Sc. Juan Pablo Carbajal
-----
PhD Student
University of Zürich
http://ailab.ifi.uzh.ch/carbajal/


reply via email to

[Prev in Thread] Current Thread [Next in Thread]