Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Vectors in matlab

  1. Nov 20, 2009 #1

    I have a 3*3 matrices in matlab:

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

    and i wont 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

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

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

  2. jcsd
  3. Nov 20, 2009 #2
    Your problem is with the for loop not doing what you think it's doing. When you say

    Code (Text):

    for i = 1:3
        a_i = A(:, i);
    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.
  4. Nov 20, 2009 #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?
  5. Nov 20, 2009 #4
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook