1:
A) Given the following vectorized code:
>>x=[1:10];
>>f=x.^2+2;
Rewrite this code using a for loop that defines each element of f
one at a time. Make sure to preallocate memory to f before filling
each spot.
B)
See the following code. Rewrite the code in one line using the
find function and a For loop. then write it again using a while
loop
x=[-1 3 7 2 4 0];
v=[];
for i=1:length(x)
if x(i)<=2
v=[v, x(i)];
end
end
please help using MATLAB
Ans
code:-
%A)
f=[];
for x=1:10
f=[f,x^2+2];
end
%B)
x=[-1,3,7,2,4,0];
v=[];
%in single line using find
v=x(find(x<=2,length(x)));
%using while
i=1;
while i<=length(x)
if x(i)<=2
v=[v,x(i)];
end
end
If any doubt ask in the comments.
Get Answers For Free
Most questions answered within 1 hours.