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:
help pls

Subject: help pls

From: anil chandy

Date: 11 Feb, 2009 06:24:01

Message: 1 of 4

is there any method to make a the columns repeat in matrix.. ie i ve a matrix wit values
0.012727 0.0095914 0.014579
0.019345 0.025261 0.0071265
0.058444 0.058444 0.045653
0.91262 0.91262 0.93264 etc.......and another matrix wit values
   

43
23
45
etc
 i weanna make the first column repeat 43 times ,2nd column 23 times 3rd one 45 times.. can anyone pls help me out....

Subject: help pls

From: Caroline From France

Date: 11 Feb, 2009 08:25:04

Message: 2 of 4

Hi,

Take a look at the help of REPMAT function.

Caroline

Subject: help pls

From: Roger Stafford

Date: 11 Feb, 2009 08:43:01

Message: 3 of 4

"anil chandy" <anilvchandy@gmail.com> wrote in message <gmtqu1$1os$1@fred.mathworks.com>...
> is there any method to make a the columns repeat in matrix.. ie i ve a matrix wit values
> 0.012727 0.0095914 0.014579
> 0.019345 0.025261 0.0071265
> 0.058444 0.058444 0.045653
> 0.91262 0.91262 0.93264 etc.......and another matrix wit values
>
>
> 43
> 23
> 45
> etc
> i weanna make the first column repeat 43 times ,2nd column 23 times 3rd one 45 times.. can anyone pls help me out....

 Let the matrix be called A and the column vector B. Assuming the rows in B match the columns in A and all its elements are non-negative integers, do this:

 p = cumsum(accumarray(cumsum([1;B]),1,size(B,1)+1));
 C = A(:,p(1:end-1));

C should be the desired result. Zeros in B are okay.

Roger Stafford

Subject: help pls

From: Jos

Date: 11 Feb, 2009 09:09:01

Message: 4 of 4

"anil chandy" <anilvchandy@gmail.com> wrote in message <gmtqu1$1os$1@fred.mathworks.com>...
> is there any method to make a the columns repeat in matrix.. ie i ve a matrix wit values
> 0.012727 0.0095914 0.014579
> 0.019345 0.025261 0.0071265
> 0.058444 0.058444 0.045653
> 0.91262 0.91262 0.93264 etc.......and another matrix wit values
>
>
> 43
> 23
> 45
> etc
> i weanna make the first column repeat 43 times ,2nd column 23 times 3rd one 45 times.. can anyone pls help me out....

An example:

DATA = [10 20 30 ; 11 21 31 ; 12 22 32 ; 13 23 33 ; 14 24 34]

We want to have column 1 to be present 3 times, column 2 once, and column 3 twice:

R = [3 1 2] ;

We might do this by hand:

idx = [1 1 1 2 3 3] ;
newdata = DATA(:,idx)

The problem has become: how to derive idx from R. One approach, separated in multiple commands for clarity:

R = [3 1 2] ;
p = cumsum(R)+1 % where does the next column start
idx = zeros(1,p(end)) % initialize
idx(p) = 1 % set the positions to 1
idx = idx(1:end-1) ; % ignore the last one
idx = cumsum(idx)
idx = idx+1
newdata2 = DATA(:,idx)

This code can be condensed.

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