Create Vector Variables in MATLAB

In summary: A{2,2};This will create a two element matrix, with each row corresponding to one element of the temp matrix. You can use the same syntax to create a three element matrix, or any other number of elements.
  • #1
Dollydaggerxo
62
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
  • #2
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.
 
  • #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.
 
  • #4
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.
 
  • #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!
 
  • #6
How do i convert '[ (2*v1)/7, -(3*v2)/7]' into (2*v1)/7 -(3*v2)/7 ?
 
  • #7
Hmmm... Can you just add the two matrix elements?

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

What is a vector variable in MATLAB?

A vector variable in MATLAB is a data type that represents a one-dimensional array of numbers. It can hold multiple values and is often used to store and manipulate data in scientific and engineering applications.

How do I create a vector variable in MATLAB?

To create a vector variable in MATLAB, you can use the "vector" function or simply assign values to a variable using square brackets, separating each value with a comma. For example, "my_vector = [1, 2, 3, 4, 5]" will create a vector variable named "my_vector" with five elements.

Can I create a vector variable with non-numeric values?

Yes, you can create a vector variable with non-numeric values in MATLAB. These can include characters, strings, and even other data types such as logical or cell arrays. However, the elements in a vector variable must all be of the same data type.

How do I access and manipulate elements in a vector variable?

To access and manipulate elements in a vector variable, you can use indexing. Indexing allows you to specify which element or elements you want to retrieve or modify. You can use single or multiple indices, and you can also use logical or conditional expressions to select specific elements.

Can I perform mathematical operations on vector variables in MATLAB?

Yes, you can perform mathematical operations on vector variables in MATLAB. In fact, vector variables are often used for mathematical computations, such as vector addition, subtraction, multiplication, and division. MATLAB also has built-in functions for various mathematical operations on vector variables.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Back
Top