0

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) = <insert function here>

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

2
Contributors
1
Reply
4
Views
6 Years
Discussion Span
Last Post by Thisisnotanid
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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.