0

I'm having problems with the following task. I have a dummy array of zeros and 2 vectors of equal size. For example:

array1 = zeros(750,1);
vector1 = [1;3;5];
vector2 = [100;250;400];

I am looking to fill array1 as follows:

repeat element 1 in vector1 100 times
repeat element 2 in vector2 250 times
repeat element 3 in vector1 400 times

The resulting vector should have 7 rows and 1 column. I tried playing around with repmat but can't get it to output only 1 dimension. I also heard about bsxfun but I never end up with the data I need. I'm grateful for any useful suggestions.

I have Matlab 2013, so I'm not able to use the fancy function repelem that I found might be useful.

Kaly
  • 3,289
  • 4
  • 24
  • 25
  • So you want to have the 100 first values of `array1` equals to 1, 250 next values equals to 3, 400 next values equals to 5, and the other values equal to 0? – Ikaros Sep 02 '15 at 12:59
  • I think he made a typo and means to put only `vector1` elements into `array1`, then the duplicate is indeed correct. – Adriaan Sep 02 '15 at 13:30

2 Answers2

1
array1(1:100) = vector1(1);
array1(101:350) = vector2(2);
array1(351:750)=vector1(3);

though why the total length is 2850 is beyond me.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
0

Maybe something like:

    vector1 = [1;3;5];
    vector2 = [100;250;400];
    temp = linspace(1,sum(vector2),sum(vector2))';
    array1 = zeros(size(temp));

    for ii = 1:length(vector2)
        array1 = array1 + (temp <= sum(vector2(1:ii)) & not(array1))*vector1(ii);
    end
    clear temp ii
JimPanse
  • 23
  • 2