Create Vector Variables in MATLAB

Click For Summary

Discussion Overview

The discussion revolves around creating a vector of symbolic variables in MATLAB, specifically how to define a variable number of symbolic variables based on the dimensions of a given matrix. Participants explore methods for initializing these variables without assigning values and how to manipulate them in mathematical expressions.

Discussion Character

  • Exploratory
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant seeks to create a vector of symbolic variables (v1, v2, v3, etc.) that corresponds to the number of columns in a matrix A, without assigning values to these variables.
  • Another participant suggests that MATLAB allows for dynamic resizing of matrices and proposes using the size() function to determine dimensions and initialize a matrix with zeros().
  • A participant clarifies the need for the vector to have the same number of elements as the columns of matrix A, emphasizing that these should be arbitrary symbols rather than assigned values.
  • There is a question about whether the variables are placeholders for numbers or symbolic variables, leading to a suggestion about using cell arrays to store mixed data types.
  • One participant expresses a desire for a way to automatically generate a specified number of symbolic variables based on the number of columns in the matrix, acknowledging the limitation of not being able to create variables in that manner directly.
  • A later post asks about converting a specific expression format into a different mathematical representation, indicating a need for clarity in expression manipulation.

Areas of Agreement / Disagreement

Participants express varying degrees of understanding regarding the creation and manipulation of symbolic variables in MATLAB. There is no consensus on a definitive method to achieve the desired outcome, and multiple approaches are discussed.

Contextual Notes

Participants mention limitations regarding the creation of symbolic variables dynamically and the potential for runtime errors if the dimensions do not match. The discussion also highlights the distinction between symbolic variables and numerical placeholders.

Who May Find This Useful

This discussion may be useful for MATLAB users interested in symbolic computation, particularly those looking to manipulate variable-length symbolic expressions or create dynamic variable names based on matrix dimensions.

Dollydaggerxo
Messages
60
Reaction score
0
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 don't 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 !
 
Physics news on Phys.org
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.
 
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.
 
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.
 
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!
 
How do i convert '[ (2*v1)/7, -(3*v2)/7]' into (2*v1)/7 -(3*v2)/7 ?
 
Hmmm... Can you just add the two matrix elements?

temp = A{1,1}+A{1,2};
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
8K