Matlab Trainings — Matlab Onramp

Summer Nie
3 min readJul 3, 2022

--

density = data(:,2)
[~,ivMax] = max(v2)
densityMax = density(ivMax)

Create a matrix named x that

  • Contains random integers in the range from 1 to 20
  • Has 5 rows
  • Has 7 columns

x = randi(20,5,7) — use help to search for the randi function

You can also open the documentation using the doc function. Try opening the documentation for randi with the code below:

doc randi

Plot

Create a plot with sample on the x-axis and mass1 on the y-axis.

plot (sample, mass1)

Plot mass2 (y-axis) against sample (x-axis). Use red (r) star (*) markers and no line in your plot.

Plot (sample, mass2,”r*”)

Enter the hold on command.

Then plot mass1 (y-axis) against sample (x-axis) with black (k) square (s) markers and no line.

hold on

plot(sample, mass1,”ks”)

To return to the default plot behavior, where each plot gets its own axes, enter hold off.

hold off

Plot a single vector by itself

plot(v1)

Plot v1 with a line width of 3.

plot(v1,”LineWidth”,3)

Plot v1 (y-axis) against sample (x-axis) with red (r) circle (o) markers and a solid line (-). Use a line width of 4.

plot(sample,v1,’ro’,”LineWidth”,4)

Add the title “Sample Mass” to the existing plot.

title(‘Sample Mass’)

Use the ylabel function to add the label “Mass (g)”.

ylabel(‘Mass (g)’)

You can add a legend to your plot using the legend function.

legend(“a”,”b”,”c”)

You can use a variable’s value in plot annotations by concatenating a string with a variable:

bar(data(3,:))
title(“Sample “ + sample(3) + “ Data”)

returns the number of elements, n, in array A,

prod(size(A)) OR

n = numel(A)

You can use the fft function to compute the discrete Fourier transform of a vector.
fft(y)

y = fft(y) — A Fourier transform will return information about the frequency content of the signal. The location of the dominant frequencies will show what notes are contained in the chord.

You can use the xlim function to zoom in on the area of interest.

xlim([xmin xmax])

To extract a variable from the table, you can use dot notation:

data.VariableName

Try extracting the first three rows of the table:

top3 = elements(1:3,:)

You can compare a vector or matrix to a single scalar value using relational operators. The result is a logical array of the same size as the original array.

[5 10 15] > 12

ans = 0 0 1

extract all elements in v1 that are greater than six.

v = v1(v1 > 6)

v = 6.6678 9.0698

Create a variable s that contains the elements in sample corresponding to where v1 is less than 4.

v = v1(v1<4)

replace all values in the array x that are equal to 999 with the value 1, use the following syntax.

x(x==999) = 1

To find values less than 4 and greater than 2, use &

x = v1(v1<4 & v1>2)

To find values greater than 6 or less than 2, use |

x = v1(v1>6 | v1<2)

If else statement

executes the code in a loop for 7 times.

for idx = 1:7

hold on

plot(idx,density(idx),”*”)

hold off

pause (0.2)

end

Loop over a vector of unknown length

for idx = 1:length(density)

Plot the spectra (s) as a function of wavelength (lambda), using log scales on both axes. Use point markers (.) and a solid line (-) connecting the points.

loglog(lambda, s,”.-”)

Add a point to the existing graph by plotting x = lambdaHa, y = sHa as a red square (“rs”) with a marker size (“MarkerSize”) of 8.

hold on

plot(lambdaHa,sHa,”rs”, “MarkerSize”, 8)

hold off

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response