In: Computer Science
The following code must be written using matlab
How to loop through a vector in matlab and assigning a value to every 4th entry. The vector could be of any length.
Thanks
I have provided the solution below, please feel free to ask if you need to add something else in comments below.
Code to copy paste:
function loopingVector(n,x) % here n is the length of vector and x is the value you want
% substiute
vec = [1:n]
%basically here is the code you wanted
for i = 4:4:n % starting the loop from 4th entry and taking a jump of 4 again
% till the end of vector
vec(i) = x; % substitute value x at every 4th entry
end
% here is something extra(that you didn't ask)
%this is how to print a vector
fprintf('The vector is: [');
fprintf('%g ', vec);
fprintf(']\n');
end
Please upvote if you find the solution helpful, thanks.