How to create a matrix from vectors in Matlab

Click For Summary
SUMMARY

This discussion addresses the creation of a 100x3 matrix in Matlab from three-dimensional row vectors. The user initially attempted to build the matrix using a loop but faced inefficiencies and confusion regarding indexing. The solution involves pre-allocating the matrix with R = zeros(100, 3) and populating it directly with R(n,:) = [i(n), j(n), k(n)]. Additionally, vector operations can be utilized to streamline the process by combining the vectors directly with R = [i(:), j(:), k(:)].

PREREQUISITES
  • Understanding of Matlab syntax and indexing
  • Familiarity with matrix operations in Matlab
  • Knowledge of vector manipulation in Matlab
  • Basic programming concepts such as loops and array initialization
NEXT STEPS
  • Learn about Matlab array pre-allocation techniques
  • Explore vectorization in Matlab for performance optimization
  • Study Matlab indexing methods for efficient data retrieval
  • Investigate the use of the zeros function for matrix initialization
USEFUL FOR

Matlab users, data analysts, and engineers looking to efficiently manage and manipulate matrices and vectors in their programming tasks.

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
Glad that you found the solution.
 
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,:).
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
Replies
3
Views
2K