MATLAB Answers

Basic queries Matlab (vectors, indexing, construct matrices in a specific form)

1 view (last 30 days)
Hello community,
I am beginner in Matlab. I am struggling with the following exrcises. I only managed to answer 1 & 5 however the others confuse me and I don't even know how to start specially with 2 & 3. Please I need your help
My solutions:
%Exercise 1
x = [2:3:89]
%Exercise 5
function density=ExamC()
x=input('Please enter x: ');
m=input('Please enter mean: ');
s=input('Please enter standard deviation: ');
f=1/(s*sqrt(2*pi))*exp(1)^((1/2)*((x-m)/s)^2);
disp('Value of density is: '), disp(f);
end
Thank you for your help,
Abdel

  5 Comments

Show 2 older comments
Stephen Cobeldick
Stephen Cobeldick on 24 May 2020
"Specially question 2. It would be nice of you if you can simplify in simple words what's exactly required or the logic to answer it."
Question 1 defines a vector named vec.
Question 2 requires you to change the even-indexed elements of vec to 5.
Abderrahmane Elakhiri
Abderrahmane Elakhiri on 24 May 2020
Thank you very much @Stephen Cobeldick!
It was very easy to do:
%Exercise 2:
vec = [1 2 3 4 5 6 7 8 9 10];
vec(rem(vec,2)==0)=5;

Sign in to comment.

Accepted Answer

per isakson
per isakson on 24 May 2020
Exercise 1 and 2
"[...] however it confuses me the way the question is written!" Agree; the very same vector has three different names, x, vec and v. (It might be about level of abstraction; an effort to distiguish between the vector and its representation in Matlab.)
Exercise 3.
The text is ok.
Exercise 4.
There are relevant examples in the documentation on plot()
Exercise 5.
The statement
function density=ExamC()
doesn't follow the instruction. The last sentence says
function f = density( x, m , s )
(to my best understanding)

  1 Comment

Abderrahmane Elakhiri
Abderrahmane Elakhiri on 27 May 2020
Thank you and apologize for the late reply.
I managed to solve all of them, and you are right for question 5, it's:
function f = density (x, m, s)
I used ExamC instead of density on purpose.
Solutions for all exercises I posted in case someone needs them:
%Exercise 1
x = [2:3:89]
%%
%Exercise 2
vec = [1 2 3 4 5 6 7 8 9 10];
vec(rem(vec,2)==0)=5; % solution 1
vec(2:2:end)=5 % solution 2
%%
%Exercise 3
A=rand(1,5); B=rand(4,2); C=rand(2,3); D=rand(2,3);
E=[A;[B,[C;D]]];
%%
%Exercise 4
x=linspace(-4,8,200);
y=sin(x);
y2=sin(2*x);
y3=sin(3*x);
plot(x,y,'red',x,y2,'black',x,y3,'green')
ylim([-1.5 1.5]);
%%
%Exercise 5
function f=density()
x=input('Please enter x: ');
m=input('Please enter mean: ');
s=input('Please enter standard deviation: ');
f=1/(s*sqrt(2*pi))*exp(1)^((1/2)*((x-m)/s)^2);
disp('Value of density is: '), disp(f);
end
%%
Note: There are probably better ways to answer some of these questions.
Regards,
Abdel

Sign in to comment.

More Answers (0)


Translated by