Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Matlab program that uses for loops and if statements to create the follo

ID: 3675472 • Letter: W

Question

Write a Matlab program that uses for loops and if statements to create the following matrix. B, that has 3 rows and 50 columns. Note that the last row of B has value of 'zero' if the value in the corresponding value in the first row is odd and has a value of 1 if the corresponding value in the first row is even. Make the program output your variable "B"' to the command window by issuing the command "B" on the last line of your code. Print out and turn in a hardcopy of your code and the output to the command window.

Explanation / Answer

B = zeros(3, 50); % Matrix B
for i =1:3       % Iterating through rows
    for j=1:50                                  % Iterating through Columns
        if i==1                                   % filling first row
            B(i, j) = j;
        elseif i==2                            % filling 2nd row
            B(i, j) = j-1;
        else                                       % filling 3rd row
            if mod(B(i-2, j), 2) == 1
                B(i, j) = 0;
            else
                B(i, j) = 1;
            end
        end
    end
end
B                                                 % printing B

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote