Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
transform vector in matrix with more elements

Subject: transform vector in matrix with more elements

From: Emily Bauner

Date: 23 May, 2010 22:15:11

Message: 1 of 6

Hi all,

I'm looking for a fast way to distribute vector elements in a given
matrix such that identical elements are listed in the same matrix row;
the tricky part is that the vector has fewer elements than the matrix.
For instance, assume that I have vector A:

A = [1 2 2 3 3 3 4 4 4 4 5 5 5 5 6 6 6 7 7 8]

I need some command that gives me matrix B, where B is

B =

     1 0 0 0
     2 2 0 0
     3 3 3 0
     4 4 4 4
     5 5 5 5
     6 6 6 0
     7 7 0 0
     8 0 0 0

This operation is very time-sensitive in my code as it has to be done
often and with varying vector/matrix sizes. Therefore I would like to
avoid a loop if at all possible.

Any ideas would be greatly appreciated.

Subject: transform vector in matrix with more elements

From: Walter Roberson

Date: 23 May, 2010 23:33:37

Message: 2 of 6

Emily Bauner wrote:
> Hi all,
>
> I'm looking for a fast way to distribute vector elements in a given
> matrix such that identical elements are listed in the same matrix row;
> the tricky part is that the vector has fewer elements than the matrix.
> For instance, assume that I have vector A:
>
> A = [1 2 2 3 3 3 4 4 4 4 5 5 5 5 6 6 6 7 7 8]
>
> I need some command that gives me matrix B, where B is
>
> B =
>
> 1 0 0 0
> 2 2 0 0
> 3 3 3 0

No performances promises, but

B = cell2mat(cellfun(@(v)
[v.',repmat(0,1,max(diff(find(diff(A))+1))-length(v))],accumarray(A.',A,[],@(v)
ans ='Uniform',0));

Except of course I would break that into two lines or more.

Subject: transform vector in matrix with more elements

From: Bruno Luong

Date: 24 May, 2010 07:01:03

Message: 3 of 6

You might use this 2 tools on FEX:

http://www.mathworks.com/matlabcentral/fileexchange/24255-consecutive-vector-spliter
http://www.mathworks.com/matlabcentral/fileexchange/22909-padcat

>> A = [1 2 2 3 3 3 4 4 4 4 5 5 5 5 6 6 6 7 7 8];
>> c = SplitVec(A);
>> B = padcat(c{:})

B =

     1 NaN NaN NaN
     2 2 NaN NaN
     3 3 3 NaN
     4 4 4 4
     5 5 5 5
     6 6 6 NaN
     7 7 NaN NaN
     8 NaN NaN NaN

>> B(isnan(B)) = 0

B =

     1 0 0 0
     2 2 0 0
     3 3 3 0
     4 4 4 4
     5 5 5 5
     6 6 6 0
     7 7 0 0
     8 0 0 0

% Bruno

Subject: transform vector in matrix with more elements

From: Emily Bauner

Date: 24 May, 2010 23:53:05

Message: 4 of 6

Thanks a lot, guys. I appreciate your suggestions. I actually worked
out two solutions to my problem based on your ideas and they both work
great. Thanks!

Subject: transform vector in matrix with more elements

From: Emily Bauner

Date: 25 May, 2010 00:00:21

Message: 5 of 6

Thanks so much for your suggestion, got it!


Walter Roberson <roberson@hushmail.com> wrote in message <mXiKn.30304$wV2.8403@newsfe23.iad>...
> Emily Bauner wrote:
> > Hi all,
> >
> > I'm looking for a fast way to distribute vector elements in a given
> > matrix such that identical elements are listed in the same matrix row;
> > the tricky part is that the vector has fewer elements than the matrix.
> > For instance, assume that I have vector A:
> >
> > A = [1 2 2 3 3 3 4 4 4 4 5 5 5 5 6 6 6 7 7 8]
> >
> > I need some command that gives me matrix B, where B is
> >
> > B =
> >
> > 1 0 0 0
> > 2 2 0 0
> > 3 3 3 0
>
> No performances promises, but
>
> B = cell2mat(cellfun(@(v)
> [v.',repmat(0,1,max(diff(find(diff(A))+1))-length(v))],accumarray(A.',A,[],@(v)
> ans ='Uniform',0));
>
> Except of course I would break that into two lines or more.

Subject: transform vector in matrix with more elements

From: Jos (10584)

Date: 25 May, 2010 12:00:22

Message: 6 of 6

Emily Bauner <emily.tting@gmail.com> wrote in message <4ad1b7f4-2b33-4c9b-abee-ff47df0cf603@e28g2000vbd.googlegroups.com>...
> Hi all,
>
> I'm looking for a fast way to distribute vector elements in a given
> matrix such that identical elements are listed in the same matrix row;
> the tricky part is that the vector has fewer elements than the matrix.
> For instance, assume that I have vector A:
>
> A = [1 2 2 3 3 3 4 4 4 4 5 5 5 5 6 6 6 7 7 8]
>
> I need some command that gives me matrix B, where B is
>
> B =
>
> 1 0 0 0
> 2 2 0 0
> 3 3 3 0
> 4 4 4 4
> 5 5 5 5
> 6 6 6 0
> 7 7 0 0
> 8 0 0 0
>
> This operation is very time-sensitive in my code as it has to be done
> often and with varying vector/matrix sizes. Therefore I would like to
> avoid a loop if at all possible.
>
> Any ideas would be greatly appreciated.

Here is another idea, vectorized and pretty quick without explicit for-loops, using a few low-level built-in ML commands:

% data
  A = [1 2 2 3 1 1 1 1 2 2 6 6 6 7 7 8] ;

% engine (based on my NONES submission on the FEX)
  n = diff([0 find(diff(A)~=0) numel(A)])
  B = cumsum([n ; repmat(-1,max(n)-1,numel(n))])
  B(B<0) = 0
  B(B>0) = A
  B = B.'

hth
Jos

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us