[Top][All Lists]

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

remove for-loop from following code

From: Stefaan Himpe
Subject: remove for-loop from following code
Date: Sat, 05 Nov 2011 15:16:39 +0100
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv: Gecko/20111010 Iceape/2.0.14

Hello all,

I have a question about optimizing my code.

I start from Y = [ 1 ; 3; 2 ]
I want to end up with Ytr = [ 1 0 0 0 ; 0 0 1 0 ; 0 1 0 0 ]
(so the number in Y indicates the column in which I want to see a one; the number of elements in each row can is given in the following code as a parameter D)

For now I do the following

function [Ytr] = ytransform(D, Y)
  [rows, cols] = size(Y);
  for row = 1:rows
    Ytr(:,row) = (1:D == Y(row));

Is there a way to do implement this without for loop over all rows of Y?

Best regards & thanks for this wonderful software.

reply via email to

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