[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
creation of a permutation matrix (WAS: Re: remove for-loop from followin
From: |
c. |
Subject: |
creation of a permutation matrix (WAS: Re: remove for-loop from following code) |
Date: |
Sun, 6 Nov 2011 10:33:01 +0100 |
On 6 Nov 2011, at 09:52, Stefaan Himpe wrote:
> 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',:);
> endfunctio
I am a bit confused,
according to the documentation your code should produce a permutation matrix,
but it seems the result is actually a full matix:
------------------------------
>> K = 5;
>> Y = [1; 3; 2];
>> Ytr = eye(K)(Y',:)
Ytr =
1 0 0 0 0
0 0 1 0 0
0 1 0 0 0
whos Ytr
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
Ytr 3x5 120 double
Total is 15 elements using 120 bytes
------------------------------
so it stores all the zeros, consuming unnecessary memory.
I think this might be a bug and I'll look further into it when I have time,
in the meantime if you want to work around the issue you can create a sparse
matrix of logicals instead:
------------------------------
>> Ytr2 = sparse (1:numel(Y), Y, true (numel (1), 1), numel(Y), K)
>> whos Ytr2
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
Ytr2 3x5 39 logical
------------------------------
c.