954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

MATLAB: Simple Vector Push

Hi,

If i have a vector in MATLAB v;

v = [1 2]


How can i push a value say '7' to the vector.
ie.

v =
1
2
7

I am currently doing it in a loop:

count = 0

ie. for 1 to 10
count = count + 1;
v(count) =

which is not ideal
sorry for poor syntax, trying to help out a mate who does matlab, i'm a c++, java guy...

John Linux
Light Poster
28 posts since May 2010
Reputation Points: 30
Solved Threads: 0
 

This article lists many operations you can perform on vectors in MATLab: http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d-arrays-vectors/ The operation I think of most help to you would be the following:

% Given a vector, 'v', we can append values to it in the following manner:

v = [1 2] % Original vector.

v = [v 7] % New vector with value appended at the end.

v = [3 v] % New vector with value appended at the beginning.


There are many interesting things you can do with vectors in MATLab, on par with the more dominant interpreted languages out there like Python. For example, you can create nested expressions or do vector operations that test for conditions. Take for instance:

v = [2 5 3 6  12 3 6 2 23 45 6523 4 123];

v(mod(v, 2) == 0) % This will print all even elements of v.


Hope it helps.

Thisisnotanid
Junior Poster in Training
60 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: