1

I am completely new to MATLAB.This may be a rather basic question.

Given numerical values for size, extras and max, I need to initialize a 1 X N vector such that the first size elements are 1, the next size are 2, the next size are 3 and so on till the last size elements are set to max. So I need to initialize size number of elements successively to x such that x increments from 1 to max. The extras are the number of leftover cells which are initialized to 0. To illustrate:

size = 3; %# (is same as the quotient of N/max)
extras = 1; %# (is same as remainder of N/max) 
max = 3; 
N = 10;

original_vector = [0 0 0 0 0 0 0 0 0 0];

The desired output is

Required_vector = [1 1 1 2 2 2 3 3 3 0]
atlantis
  • 3,056
  • 9
  • 30
  • 41
  • 2
    It is best to avoid using 'max' and 'size' as variable names since these are names of Matlab functions. – MatlabSorter Sep 28 '11 at 00:10
  • @MatlabSorter Good advice...Just used them here for example..my actual code has domain specific names. – atlantis Sep 28 '11 at 00:43
  • similar question: [Element-wise array replication in Matlab](http://stackoverflow.com/questions/1947889/element-wise-array-replication-in-matlab) – Amro Sep 28 '11 at 02:16

4 Answers4

1

Maybe something using the Kronecker product:

N = 10;
max = 3;
extras = rem(N, max);
size = floor(N/max);

v = [kron([1 : max], ones(1,size)) zeros(1, extras)];

I took a guess about how extras and size are calculated. You said size is N % max and extras is N rem max, but those are the same thing(?).

dantswain
  • 5,427
  • 1
  • 29
  • 37
  • ok...my bad...I got confused about the %...so dividing N by max, size is the quotient and extras the remainder – atlantis Sep 28 '11 at 00:41
1

Some reshaping acrobatics should do it:

>> size = 3;
>> max = 3;
>> N = 10;
>> v = zeros(1, N);
>> v(1:size*max) = reshape(cumsum(ones(max, size))', size*max, 1)

v =

     1     1     1     2     2     2     3     3     3     0

Another example:

>> size = 4;
>> max = 5;
>> N = 23;
>> v(1:size*max) = reshape(cumsum(ones(max, size))', size*max, 1)

v =

  Columns 1 through 18

     1     1     1     1     2     2     2     2     3     3     3     3     4     4     4     4     5     5

  Columns 19 through 23

     5     5     0     0     0
b3.
  • 7,094
  • 2
  • 33
  • 48
0

This is a quite dirty implementation, but as you say you are very new to MATLAB, it might be better for you to see how you can more or less brute force a solution out. The trick here is the index reference done on Vec to place the numbers in. I have ignored the parameter extras and instead fill the vector up as best can be with the elements

N = 23;
max = 3;
size = 4;

Vec = zeros(N,1);
for i=1:max
    for j=1:size
       Vec((i-1)*size +1 + (j-1)) = i;
    end
end

Vec'
extra = sum(Vec==0)

Output: ans =

1  1  1  1  2  2  2  2  3  3  3  3  0  0  0  0  0  0  0  0  0  0  0

extra =

11
Vidar
  • 4,141
  • 5
  • 24
  • 30
  • Couple of things: size and extras aren't arbitrary..they depend on N and max. size is same as N % max and extras is same as N 'rem' max. Past that, I agree this implementation may work...but I just cringe at the thought of having to use nested for loops in matlab code. Isn't there a better way? – atlantis Sep 27 '11 at 21:39
  • You could replace the inner loop with `Vec((i-1)*size + [1 : size]) = i*ones(1,size);`. – dantswain Sep 27 '11 at 22:01
0

A slight modification of @b3's solution:

N = 10;
mx = 3;

sz = floor(N/mx);
v = zeros(1,N);
v(1:mx*sz) = repmat(1:mx,sz,1)
Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454