6. Write a Matlab program using FOR loop that prints a given row or column vecto
ID: 3807775 • Letter: 6
Question
6. Write a Matlab program using FOR loop that prints a given row or column vector in reverse. Load Array1.m .mat and use Array2 as a sample for testing row vector and use Array3 as a sample to test the functionality of reversing a column vector. Use the length command to know the elements in the array. Display matrix using following command disp and mat2str.
x = [8 4 6 2]; disp(['The original array was: ' mat2str(x)]);
Array1 =
71 3 28 5 10 82 69 32 95 3 44 38 77 80 19
Array2 =
7 5 10 3 11 4 1 12 15 2
Array3 =
7
5
10
3
11
4
1
12
15
2
Explanation / Answer
Row Matrix:
Array2=[71 3 28 5 10 82 69 32 95 3 44 38 77 80 19 ];
for i = 1:length(Array2)
RevArray(i) = Array2(length(Array2)-i+1);
end
disp("Array is ")
disp(Array2)
disp("Reverse of Row matrix ")
disp(mat2str(RevArray))
Column vector:
We use the same logic,only the array changes as it is column vector we give inputs in columns i.e ; separated
Array3=[7; 5; 10; 3; 11; 4; 1; 12; 15; 2 ];
for i = 1:length(Array3)
RevArray(i) = Array3(length(Array3)-i+1);
end
disp("Array is ")
disp(Array3)
disp("Reverse of Column matrix ")
%disp(RevArray')
disp(mat2str(RevArray'))%mat2str format
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.