MATLAB Help: Study this example. Suppose you have 2 vectors of the same length,
ID: 3725355 • Letter: M
Question
MATLAB Help:
Study this example. Suppose you have 2 vectors of the same length, LO = [1 5 12]. and HI = [3 8 10]. The program should create this vector [1 2 3 4 5 6 7 8], where its elements are numbers between each pair of LO(i) and HI(i): from 1 to 3, and from 5 to 8. Pairs where LO(i)>HI(i), such as from 12 to 10, should be deleted and result in no numbers.
Write a MATLAB program that will first generare a random integer between 3 and 6 as the length of the LO/HI vectors; it will then generate the LO vector of random integers between 1 and 10 and the HI vector of random integers between 5 and 15; it will traverse LO and HI to create the vector consisting of numers running from LO(i) to HI(i). Example below.
-----------------------
The LO vector is [ 5 6 2 6 ]
The HI vector is [ 9 9 3 5 ]
The final vector is [ 5 6 7 8 9 6 7 8 9 2 3 ]
-----------------------
Explanation / Answer
PLEASE REFER BELOW CODE
close all
clear all
clc
%generating random integer between 3 and 6 for LO and HI length
LO_length = randi([3 6],1,1);
HI_length = LO_length;
%Generating vector between numbers 1 and 10 of size LO length
LO= randi([1,10],1,LO_length);
%Generating vector between numbers 5 and 15 of size HI length
HI = randi([5,15],1,HI_length);
%resultant vector
RES=0;
%index of resultant vector
k = 1;
%travel through LO and HI vector
for i = 1:LO_length
if LO(i) < HI(i)
j = 0;
%adding element to final vector
while j <= (HI(i) - LO(i))
RES(k) = j + LO(i);
j = j+1;
k = k + 1;
end
end
end
%displaying final vector
fprintf("The LO vector is ");
disp(strrep(['[' sprintf(' %d ', LO) ']'], ',)', ']'));
fprintf(" The HI vector is ");
disp(strrep(['[' sprintf(' %d ', HI) ']'], ',)', ']'));
fprintf(" The final vector is ");
disp(strrep(['[' sprintf(' %d ', RES) ']'], ',)', ']'))
PLEASE REFER BELOW OUTPUT
The LO vector is [ 6 2 9 7 4 ]
The HI vector is [ 10 9 5 7 6 ]
The final vector is [ 6 7 8 9 10 2 3 4 5 6 7 8 9 4 5 6 ]
>>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.