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...

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.