MATLAB programming 1. Given a=[1 4 3 -1 6 1], create a vector b such that b(i)=a
ID: 642170 • Letter: M
Question
MATLAB programming
1.
Given a=[1 4 3 -1 6 1], create a vector b such that b(i)=a(i)+3 if a(i)>1, and b(i)=a(i)-2 if a(i)<=1.
2.
The vector c takes on the following values depending upon the value of b.
C=[1 2 3] if b=1, C=[6 7 8 ] if b = 2 and C=[10 14 15] if b neither of the above. Create a matrix M whose rows are the vector C when b=[1 6 2 0].
3.
For the next two weeks, the time that you spend in the library increases by 10 mins each day, starting with 20 mins on the first day. For each hour, or fraction thereof, that you are in the library, you spend $0.175 on coffee, how much money are you going to spend on coffee in the library during the second of the next two weeks?
plz help me write those programming with MATLAB.
Explanation / Answer
% answer of ques 1
clc % clear display
clear all;% clear everything
a=[1 4 3 -1 6 1] % input
b=zeros(1,length(a));% creat empty b of length a
for i=1:length(a) % loop over a
if a(i)>1
b(i)=a(i)+3;
else % if a(i)<=1
b(i)=a(i)-2;
end
end
b %print b
% answer 2
clc
clear all;
b=[1 6 2 0]
for i=1:length(b)
if b(i)==1
C=[1 2 3];
elseif b(i)== 2
C=[6 7 8 ];
else
C=[10 14 15];
end
m(i,:)=C;
end
ques 3
start_time=20;
coffee_rate_hour=0.175;
total_time=0;
old_time=start_time;
for i=1:14 % 14 days = 2 weeks
new_time=old_time+(i-1)*10;% i-1 because first day time is 20
total_time=total_time+new_time;
end
time_in_hours=total_time/60;
expenditure=time_in_hours*coffee_rate_hour
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.