Write a Matlab program using WHILE loop that prints a given
Write a Matlab program using WHILE loop that prints a given row or column vector in reverse. Load lab7.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)]);
Solution
Code:
a = [1 2 3 4 5];
i = 1;
b = [];
len = length(a)+1;
while i < len
b(i) = a(len-i);
i = i + 1;
end
disp([\'The original array was: \' mat2str(a)]);
disp([\'The reversed array is: \' mat2str(b)]);
Output:
The original array was: [1 2 3 4 5]
The reversed array is: [5 4 3 2 1]
