MATLAB Vector Assignment of Arbitrary Length

$\begingroup$

Greetings: let's suppose I have a loop that will execute 20 times, and within that loop I'll obtain a vector of information whose length will increase, each iteration, from 2 to 20. Now suppose I have a matrix of dimension 20, and during each iteration, I'd like to store that vector to one row of the matrix. For n = 2, the vector is length 2, and when I assign it to the first row of the matrix, call it A, I might try something like A(1,:) = p. But, since the length of the first row is 2o, and length of p is 2, there's a mismatch. Any clever suggestions on how to get around this issue.

Thanks so much...

John Sevic

$\endgroup$ 7

1 Answer

$\begingroup$

This can be accomplished as follows:

P = zeros(19,20); %Preallocate memory
for i = 2:20 p = someFunction(); P(1:i,i-1)=p;
end

This will store each vector p in a row of P.

$\endgroup$

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like