Question

In: Computer Science

The following code must be written using matlab How to loop through a vector in matlab...

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

Solutions

Expert Solution

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.


Related Solutions

The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals for position 1, position 2, position3 and position 4 of the array. After these first 4 positions are drawn. The whole thing should start over where position5 drawn from same interval as positions 1, position6 drawn from same interval as...
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals in chunks of 4 , that is chunk1-chunk2-chunk3-chunk4 The parameters for specifying the lintervals by which the random numbers should be drawn should be able to change and be hardcoded in the script, however, be hardcoded in the script.
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting clc, clear, format compact; %define variables k=1; b=-2; x=-1; y=-2; %while loop initialization for k <= 3 disp([num2str(k), ' ',num2str(b),' ',num2str(x),' ',num2str(y),]); y = x^2 -3; if y< b b = y; end x = x+1; k = k+1; end
These instructions are written with the assumption that code will be done in matlab. You might...
These instructions are written with the assumption that code will be done in matlab. You might find the following built in commands useful: length, plot, xlabel, ylabel, title, legend, fzero, plot, disp, axis, axes, min, max. 2. Numerical Integration (Quadrature). Write FOUR of your own numerical integration routines. One should use left-end, right-end, or mid-points, another should use the trapezoid method, another should use Simpson’s method, and the fourth should use either Guassian Quadrature or Romberg’s method. Use your four...
These instructions are written with the assumption that code will be done in matlab. You might...
These instructions are written with the assumption that code will be done in matlab. You might find the following built in commands useful: length, plot, xlabel, ylabel, title, legend, fzero, plot, disp, axis, axes, min, max. 3. Root finding; Bisection, Secant, Newton. a) Write your own version of the bisection method to solve e 2x − 10x = 0. Assume the root is in the interval [0, 1]. Try to use the matlab command ”fzero” to accomplish the same thing....
This code is to be written in Matlab. Write a function that will plot cos(x) for...
This code is to be written in Matlab. Write a function that will plot cos(x) for x values ranging from -pi to pi in steps of 0.1, using black *'s. It will do this three times across in one Figure Window, with varying line widths. If no arguments are passed to the function, the line widths will be 1, 2, and 3. If on the other hand, an argument is passed to the function, it is multiplier for these values....
Give me a MATLAB code which integrates any function using Romberg integration The code must work...
Give me a MATLAB code which integrates any function using Romberg integration The code must work fine or else I will down vote the answer badly Dont post if you dont know
VHDL code for a 4 to 2 priority encoder. The structural behavior must be written using...
VHDL code for a 4 to 2 priority encoder. The structural behavior must be written using gates.
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you...
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you cannot give correct MATLAB
How to design FIR lowpass filter using matlab. Note : Do not write matlab code. Only...
How to design FIR lowpass filter using matlab. Note : Do not write matlab code. Only explain the steps of designing filter
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT