Vector concatenation in for loop (MATLAB)

In summary, the conversation discusses a problem with concatenating vectors into a matrix in a for loop in a MATLAB program. One solution is to preallocate a matrix and append the vectors to it within the loop. Another solution is to preallocate a matrix and store the vectors in the appropriate columns.
  • #1
Prometheos
13
0
I have a program that calculates the inverse of a matrix. However, in my for loop I generate n vectors y which are nx1 vectors. The vectors are correctly calculated, but I can't figure out how to concatenate them into an nxn matrix.

The problem I think I'm having is that my vector y is generated but it isn't indexed ie y_1, y_2, or something like that. So I can't just make X=[y_1 y_2 ...]. Each time the loop runs it erases y and replaces it with the new y, so at the end I only have the nth vector left.

Does anyone know of a way to fix this so the output each loop would be [y_1] -> [y_1 y_2] -> ... n times?

This is my first program using MATLAB and I am quite stuck, thanks in advance.
 
Physics news on Phys.org
  • #2
One simple way to do it is to define a matrix to hold the vectors before entering the loop; once you're inside the loop you can then concatenate the vector to the matrix.

Code:
A = []; % This is the matrix to hold the vectors.

for i = 1:n
   % Do stuff with the vector.
   A = [A y]; % Append the vector y to A.
end

This should work as a first approximation but it suffers one major drawback: each time you append the vector to the matrix A in the loop, the matrix A is redefined; this takes a lot of time. If you know in advance how many vectors you want to store, you can preallocate the matrix A and simply redefine the necessary portion in each step of the loop.

For instance, if you wish to store n vectors, with the dimension of each being mx1, something like the following might be useful.

Code:
A = zeros(m,n); % Preallocate A to be a mxn matrix of zeros.

for i = 1:n
   % Do stuff with the vector.
   A(:,i) = y; % Store the vector y in the ith column of A.
end
 
Last edited:
  • #3
Awesome! Thank you so much Shoehorn, that worked like a charm. I thought I had fought through all the "hard" stuff in writing my program until I ran into this concatenation problem.
 

Related to Vector concatenation in for loop (MATLAB)

What is vector concatenation in for loop?

Vector concatenation in for loop is the process of combining multiple vectors into one larger vector, using a for loop in MATLAB. This allows for efficient and organized manipulation of data within a single vector.

How is vector concatenation achieved in for loop?

In MATLAB, vector concatenation can be achieved using the "cat" function within a for loop. The "cat" function takes in the dimension along which the vectors should be concatenated, and the vectors themselves as arguments.

What are the advantages of using vector concatenation in for loop?

Vector concatenation in for loop allows for efficient and organized manipulation of data, as well as simplifying code and reducing the number of variables needed. It also helps in avoiding repetitive code and makes code easier to read and understand.

Can different types of data be concatenated using for loop in MATLAB?

Yes, different types of data can be concatenated using for loop in MATLAB. However, the data must be of the same type, such as all numeric or all character arrays, in order to be concatenated together.

Are there any limitations to vector concatenation in for loop?

One limitation of vector concatenation in for loop is that it can only concatenate vectors along a specific dimension. Additionally, it can only concatenate vectors of the same data type. If these conditions are not met, an error will occur.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
849
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Linear and Abstract Algebra
Replies
34
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
Back
Top