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
    Hallo,

    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
    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
     
  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);
    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.
     
  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
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Vectors in matlab
Loading...