[Matlab] Create and fill a row vector

Solved
manu78640 Posted messages 42 Status Member -  
 Loulou&Kathhhhy -
Hello,

I want to create and fill a row vector with 20 values, how can I do that?

Best regards,

Manu78640
Configuration: Windows XP Internet Explorer 7.0

1 answer

Fee Fay Posted messages 646 Registration date   Status Member 377
 
Good evening my friend!

With Matlab, declarations are not necessary and it is faster to work in vector form. For this, most Matlab functions are used vectorially (cos, sin, log, ...) and the operators * / ^ etc... can be used element-wise by adding a . before the operators like this: .* ./ .^

If you have no choice but to fill a vector in a loop, although it is not necessary to declare its size before entering the loop, it is still faster to do so; otherwise Matlab will have to update the memory space reserved for the vector at each iteration of the loop in question; in this case, a possible solution is to initially define a vector of the desired size filled with zeros for example.

Here are a few examples of simple commands.
v=zeros(1,20); % Defines the row vector of length 20 and filled with 0 v=ones(1,20); % Defines the row vector of length 20 and filled with 1 v=(1:20); % Defines the row vector [1,2,3,...,18,19,20] v=(4:3:61); % Defines the row vector [4,7,10,...,55,58,61] v=(32:-1:13); % Defines the row vector [32,31,30,...,15,14,13] v=(55:-2:17); % Defines the row vector [55,53,51,...,21,19,17] v=linspace(0,1,20); % Defines the regular subdivision of [0,1] of length 20
And there are still many other possibilities that you will discover...

Finally, here are some additional explanations regarding these loop and vectorization stories. Suppose you want to define the vector v=[exp(1²), exp(1/2²),exp(1/3²),...,exp(1/39²),exp(1/40²)], the slowest and ugliest method would be as follows (really a horror in Matlab):
for k=1:20 v(k)=exp(1/k^2); end
The following method, although a bit better, is still a horror like the previous one:
v=zeros(1,20) % The size is known before the loop, it's better for k=1:20 v(k)=exp(1/k^2); end
And finally, the method to use is this one:
v=exp(1./(1:20).^2);
I wish you a good night!
--
All the animals were shouting loud and clear
That he was the most beautiful toad, when he played the banjo.
54
douroc
 
I'm having trouble with the ARCH test on MATLAB, can someone please explain it to me, especially regarding the residuals?
0
Loulou&Kathhhhy
 
Thank you for everything, we are in the SICOM pre-orientation lab and it really helped us a lot, thank you that's nice and see you later, biiiiiiiiig hugs!!!!!!!!!!!!!!!!!
0