How to create a matrix from vectors in Matlab

In summary, the conversation discusses creating a matrix from vectors in Matlab. The desired matrix is a 100 row by 3 column matrix, A(n), containing three dimensional row vectors with components x(n), y(n), and z(n). The conversation also mentions the use of loops and vector operations to achieve this task.
  • #1
gnurf
370
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
  • #3
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,:).
 

1. How do I create a matrix from vectors in Matlab?

To create a matrix from vectors in Matlab, you can use the "reshape" function. This function takes in a vector and the desired dimensions of the matrix and converts the vector into a matrix. You can also use the "cat" function to concatenate multiple vectors into a matrix.

2. Can I create a matrix with different types of data in Matlab?

Yes, Matlab allows you to create matrices with different types of data. You can use the "cell" function to create a cell array, which can hold any type of data. You can also use the "struct" function to create a structure array, which can hold different types of data in its fields.

3. How can I add or remove elements from a matrix in Matlab?

You can add elements to a matrix in Matlab using the "vertcat" or "horzcat" functions. These functions allow you to vertically or horizontally concatenate new elements onto the matrix. To remove elements, you can use the "reshape" function to resize the matrix and remove the desired elements.

4. Can I perform operations on matrices created from vectors in Matlab?

Yes, you can perform various operations on matrices created from vectors in Matlab. You can use the built-in functions for matrix operations such as addition, subtraction, multiplication, and division. You can also use the "find" function to locate specific elements in the matrix.

5. Are there any other methods for creating matrices from vectors in Matlab?

Aside from using the "reshape" and "cat" functions, you can also use the "zeros", "ones", and "eye" functions to create matrices with specific dimensions and values. Additionally, you can use the "linspace" function to create evenly spaced vectors and then reshape them into matrices.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
546
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
789
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Linear and Abstract Algebra
Replies
4
Views
837
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top