Efficiently Extract Vector from MATLAB Matrices

Click For Summary

Discussion Overview

The discussion centers around extracting individual column vectors from a 3x3 matrix in MATLAB. Participants explore methods to achieve this efficiently and clarify misunderstandings related to MATLAB syntax and behavior.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant shares a MATLAB code snippet intending to extract column vectors from a matrix but encounters an issue where only the last vector is retained.
  • Another participant explains that the problem arises from redefining a single variable, a_i, rather than creating separate variables for each column vector.
  • A follow-up question asks if there is a loop or command that can generate separate vectors a_1, a_2, and a_3 from the matrix A.
  • A suggestion is made to consider why the participant wants to break the matrix into vectors instead of keeping it as a matrix.

Areas of Agreement / Disagreement

Participants generally agree on the misunderstanding of variable assignment in MATLAB, but the discussion about the necessity of extracting vectors versus maintaining the matrix remains unresolved.

Contextual Notes

Participants do not clarify the specific requirements or constraints for the extraction process, nor do they address potential implications of using separate vectors versus a matrix.

omri3012
Messages
60
Reaction score
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
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.
 
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?
 

Similar threads

  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 8 ·
Replies
8
Views
3K