Home > Mobile >  How to fill the vector x by values taken from another small vector in Matlab
How to fill the vector x by values taken from another small vector in Matlab

Time:01-31

I have a vector X with size (16 x 1), and another randomly generated small vector y of size (4 x 1), I want to fill the first two values of y in X(2) and X(3) and the other two values in X(15) and X(16), (It means, X(2) = y(1); X(3) = y(2); X(15) = y(3); X(16) = y(4)), then the next generated vector y will be placed in X(4), X(5), X(13) and X(14) and so on. How can I do that in Matlab using for example the loop for? For example:

X = zeros(16,1)
for i = 1 : 3 
y = rand(4,1)
X(i 1) = y(1); 
X(i 2) = y(2); 
X(end-i 1) = y(4); 
X(end-1) = y(3);
...... 
end 

Now, I don't know how to place the values of the next generated vector y in next positions, which are X(4), X(5), X(end-3) and X(end-4), respectively.

CodePudding user response:

I'm not 100% sure I understand your question, but does this help:

X = zeros(16, 1);

for ii = 1:4
    y = rand(4, 1);

    X(2*ii - 1) = y(1);
    X(2*ii) = y(2);
    X(end - 2*ii   1) = y(3);
    X(end - 2*ii   2) = y(4);
end

I'm not sure this is really solving your underlying problem as I'm not clear how this approach would be better than something equivalent like:

y = rand(16, 1);
  •  Tags:  
  • Related