How to create a matrix from vectors in Matlab

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 11K views
gnurf
Messages
370
Reaction score
8
How to create a matrix from vectors in Matlab [solved]

I have n=1:100 three dimensional row vectors with components x(n), y(n), and z(n), representing a vector at hundred different instances of time. I would like to create a 100 row by 3 column matrix, A(n), that would contain these vectors, such that a call of, e.g., A(4) would return the vector with components x(4) y(4) z(4).

Apparently, I suck at using both Matlab and google, so my question is: How is this done in Matlab?

Edit:
My futile attempt so far looks like this:
Code:
for n = 1:100
    i(n) = ...
    j(n) = ...
    k(n) = ...
   
    H = [i(n), j(n), k(n)];
end;

Which of course only leaves me with H=H(100)...

Edit 2:

Adding R = [R;H]; to the line below H = ... and simply calling R(n,:) for the nth vector in question seems to do the trick, albeit not exactly the way I wanted.
 
Last edited by a moderator:
Physics news on Phys.org
gnurf said:
How to create a matrix from vectors in Matlab [solved]

I have n=1:100 three dimensional row vectors with components x(n), y(n), and z(n), representing a vector at hundred different instances of time. I would like to create a 100 row by 3 column matrix, A(n), that would contain these vectors, such that a call of, e.g., A(4) would return the vector with components x(4) y(4) z(4).

Apparently, I suck at using both Matlab and google, so my question is: How is this done in Matlab?

Edit:
My futile attempt so far looks like this:
Code:
for n = 1:100
    i(n) = ...
    j(n) = ...
    k(n) = ...

    H = [i(n), j(n), k(n)];
end;

Which of course only leaves me with H=H(100)...

Edit 2:

Adding R = [R;H]; to the line below H = ... and simply calling R(n,:) for the nth vector in question seems to do the trick, albeit not exactly the way I wanted.

A few comments.
1. In Matlab all arrays are also indexed as vectors. A(4) will be the 4th element in the storage for A, which I believe will be the 1st element of the 4th row. A(4,:) as you found is the syntax for the 4th row. Why is that not acceptable?

2. Do you want this thing to be called A, H or R? You've used all three in your question. If it's going to be called R, then the way to append a new row to R is indeed R = [R;i(n), j(n), k(n)]; No need to define H first.

3. Growing a matrix dynamically that way in Matlab turns out to be terribly inefficient as it keeps reallocating the memory. There are a couple of tricks. The simplest is to pre-allocate an empty matrix by doing this before your loop: R = zeros(100, 3). Then inside your look do this: R(n,:) = [i(n), j(n), k(n)];

4. You can do this all with vector operations after the loop that creates i, j and k. If i, j and k are already column vectors, then all you have to do is this:
R = [i, j, k];
That appends three column vectors side by side.

If they are row vectors, you have to take the transposes before doing that:
R = [i', j', k'];

and if you don't know, then there's a trick using (:). The notation i(:) will list all the elements of i, guaranteed to be in column-vector form. So you can do this regardless of the shape of i, j and k:
R = [i(:), j(:), k(:)];

Either way, the n-th row of matrix R will be R(n,:).