Efficiently Extract Vector from MATLAB Matrices

In summary, the conversation discusses a problem with generating three separate vectors from a 3x3 matrix in Matlab. The solution involves understanding how for loops work in Matlab and using the correct syntax to generate the desired vectors. The conversation also suggests considering keeping the data in a matrix instead of breaking it into separate vectors.
  • #1
omri3012
62
0
Hallo,

I have a 3*3 matrices in matlab:

A=[ 1 2 3; 4 5 6; 7 8 9];

and i won't to accept a vector from each colume in an easy way, i.e.

a1=1 2 3 , a2=4 5 6 , a3=7 8 9
so i tried to wrote:

for i=1:3
a_i=A(:,i);
end

but i only the last vector, a3=7 8 9...

if anyone have a good suggestion it would be very helpful.

thnks
Omri
 
Physics news on Phys.org
  • #2
omri3012 said:
Hallo,

I have a 3*3 matrices in matlab:

A=[ 1 2 3; 4 5 6; 7 8 9];

and i won't to accept a vector from each colume in an easy way, i.e.

a1=1 2 3 , a2=4 5 6 , a3=7 8 9
so i tried to wrote:

for i=1:3
a_i=A(:,i);
end

but i only the last vector, a3=7 8 9...

if anyone have a good suggestion it would be very helpful.

thnks
Omri

Your problem is with the for loop not doing what you think it's doing. When you say

Code:
for i = 1:3
    a_i = A(:, i);
end

you are not declaring three separate vectors a_1, a_2, a_3 and assigning them the rows of A. What you are doing is declaring a vector a_i and successively redefining it to be equal to each row of A, so that by the end of the for loop you have a_i = A(3,:). In other words, simply putting '_i' at the end of a vector isn't how you subscript vectors within Matlab.
 
  • #3
thanks for ypur responde,

i understand what you are saying, but is there a loop or other command
that generate 3 vectors a_1, a_2, a_3 from the matrices A?
 
  • #4

1. How can I efficiently extract a vector from a MATLAB matrix?

To efficiently extract a vector from a MATLAB matrix, you can use the colon operator (:) or the index operator ([]) with appropriate indices. For example, if you have a matrix A and you want to extract the second column as a vector, you can use A(:,2) or A(:,[2]). This will return a vector with all the elements from the second column of A.

2. Can I extract a specific range of values from a matrix as a vector?

Yes, you can use the colon operator (:) with a specified range of indices to extract a vector from a matrix. For example, if you want to extract the first three elements from the second row of a matrix A, you can use A(2,1:3). This will return a vector with the first three elements from the second row of A.

3. How do I extract a vector from a multi-dimensional matrix in MATLAB?

To extract a vector from a multi-dimensional matrix in MATLAB, you can use the colon operator (:) or the index operator ([]) with appropriate indices for the dimensions you want to extract. For example, if you have a 3-dimensional matrix A and you want to extract a vector from the third column of the first 2-dimensional slice, you can use A(:,1,3) or A(:,1,3). This will return a vector with all the elements from the third column of the first 2-dimensional slice of A.

4. Is there a way to extract a vector from a matrix without creating a new variable?

Yes, you can use the colon operator (:) or the index operator ([]) with the assignment operator (=) to extract a vector from a matrix without creating a new variable. For example, if you want to extract the second column of a matrix A and store it in a new variable B, you can use B = A(:,2) or B = A(:,[2]). This will extract the vector and assign it to the variable B without creating a new variable for the vector.

5. How can I extract a vector from a matrix with a logical condition?

You can use the logical indexing feature in MATLAB to extract a vector from a matrix based on a logical condition. For example, if you have a matrix A and you want to extract all the elements that are greater than 5, you can use A(A>5). This will return a vector with all the elements from A that satisfy the logical condition.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top