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

MATLAB vector variables

  1. Apr 24, 2010 #1
    Hello !

    I was wondering if somebody could possibly help me.
    I am looking to create a vector of variables. [v1 v2 v3 v4 etc...]
    I dont want to assign the value to any of them.

    Then I want to multiply this vector by another, so the final answer will be:
    z=3v1 + 4v2 + 6v3 etc....

    However, the amount of v1 v2 v3 can change each time the user runs the function. So i need the number of v's to be the number of rows in my other vector.

    I hope this is clear, and that somebody is able to help.
    Thank you !
  2. jcsd
  3. Apr 24, 2010 #2


    User Avatar
    Science Advisor
    Gold Member

    I'm not sure what you mean but Matlab is pretty lax when it comes to vectors. It will enlarge a vector or matrix if you write to an element outside of its current dimensions. This is a slow way to build up the matrix however. Still, you can also use the size() function to find the dimensions of a matrix and then use something like zeros() to initialize a matrix to a desired set of dimensions.

    So if a user passes in the vectors: v1 v2 v3, ...
    You can make a matrix out of them with: A = [v1 v2 v3 ...]

    This of course relies on all the vectors being dimension (n,1). If not, a runtime error will be thrown.
  4. Apr 24, 2010 #3
    Thanks for the reply.
    Sorry if i wasnt very clear.
    Basically, I want to create a vector of variables. the vector will have the same number of rows as the number of columns on a previous matrix, A.
    But I don't want any values assigned to the vector.

    So if [rows,cols]=size(A)
    I want to create a vector of variables. The amount of variables will be equal to 'cols'.

    So if the matrix A has 3 columns, my vector v would be [v1 v2 v3]
    if the matrix had 4 columns, my vector v would be [v1 v2 v3 v4]
    etc etc where v1, v2... have no assigned values, they are JUST ARBITARY LETTERS.

    I hope this makes it clearer.
    Would your suggestion above work for this?
    Thanks again.
  5. Apr 24, 2010 #4


    User Avatar
    Science Advisor
    Gold Member

    Are these just placeholders for numbers or are these symbols?

    If they are syms, then I do not think you can use the vector and matrix datatypes. However, I recall that there is a general "matrix" data structure that you can use that accepts any kind of datatype. These are called cell arrays and are constructed using the braces as opposed to the brackets. For example, I believe it goes like this,

    syms v1, v2, v3, v4;
    A = {v1 v2 v3 v4};
    A{1,3} = v3;

    You can store whatever you like and you can mix datatypes and data structures as well.
  6. Apr 24, 2010 #5
    thankyou, yes this is what i want I think, except if the cols of the matrix is like 7.
    I want it to automatically create seven things.

    so basically i need like:

    syms v1:vcols
    A = {v1:cols};

    but i know I cannot do this!
  7. Apr 24, 2010 #6
    How do i convert '[ (2*v1)/7, -(3*v2)/7]' into (2*v1)/7 -(3*v2)/7 ?
  8. Apr 24, 2010 #7


    User Avatar
    Science Advisor
    Gold Member

    Hmmm... Can you just add the two matrix elements?

    temp = A{1,1}+A{1,2};
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook