how to plot column vector data along y-axis in matlab? - matlab

I have this row/column vector.
grades = [90, 100, 80, 70, 75, 88, 98, 78, 86, 95, 100, 92, 29, 50];
plot(grades);
In MATLAB, I want to plot grade values along x-axis and indices (1-14) along y-axis. By deafult, indices are plotted along x-axis. How it can be achieved?

grades = [90, 100, 80, 70, 75, 88, 98, 78, 86, 95, 100, 92, 29, 50];
figure;
plot(1:length(grades),grades); % Indices along X
figure;
plot(grades,1:length(grades)); % Indices along Y

If you want to plot data in Matlab. You have to define data sets for all the axis which you are interested in.
In your case, define x-axis data and y-axis data.
So for example your Y axis data would be
grades = [90 100 80 70 75 88 98 78 86 95 100 92 29 50];
for your x data you can use the following.
X = 1:14;
then you have the following plot command
plot(x,grades)

Related

Understanding Matlab histcounts behavior

histcounts(1:100,'BinWidth',50)
returns
49 51
Why doesn't it return
50 50
instead?
Histogramming 1 to 100 inclusive with h = histogram(1:100, 'BinWidth', 50) gives:
Let's see the bin edges:
h.BinEdges
ans =
0 50 100
From MATLAB's help:
Each bin includes the left edge, but does not include the right edge,
except for the last bin which includes both edges
That means that values 1 to 100 are histogrammed in this format:
Bin 1 => edges: [0 50) => Included values: [1, 2, 3, .., 49] (n = 49)
Bin 2 => edges: [50 100] => Included values: [50, 51, 52, .., 100] (n = 51)
histcount(X) partitions X in the same manner as histogram(X). Therefore, the results are what you should expect and in fact very reasonable.

Sort 2 arrays/vectors based off 1 vector

I have 2 arrays (vectors? in m vernacular?) and I want to sort them in unison. How can I achieve this in Matlab?
For example; I have found the peaks from a histogram and they are stored in 2 arrays; peakXVals, peakYVals. They will always be arranged in ascending x axis index. So they will always look like:
peakXVals = [0, 3, 20, 77, 240];
peakYVals = [10, 999, 30, 40, 20];
I wish to sort both arrays based of the values in peakYVals in descending order. Ie from largest peak to smallest peak. So the desired result is:
peakXVals = [3, 77, 20, 240, 0];
peakYVals = [999, 40, 30, 20, 10];
What function's can I use to achieve this in Matlab?
Use sort:
peakXVals = [0, 3, 20, 77, 240];
peakYVals = [10, 999, 30, 40, 20];
>> [B,I] = sort(peakYVals, 'descend')
B =
999 40 30 20 10
I =
2 4 3 5 1
Then:
>> peakXVals_sorted = peakXVals(I)
peakXVals_sorted =
3 77 20 240 0
>> peakYVals_sorted = B
peakYVals_sorted =
999 40 30 20 10
You can arrange the two vectors as columns of a matrix and sort the rows of that matrix as atoms, in lexicographical order. Then the results are the columns of the sorted matrix:
tmp = sortrows([peakYVals(:) peakXVals(:)], 'descend');
peakYVals = tmp(:,1).';
peakXVals = tmp(:,2).';

how do i plot contourf given no relationship between X, Y and Z variables?

I have a dataset in which I would want to plot in matlab. The main dataset is the result of the differences between two angles (Øact - Øcalc) obtained from analytical and experimental results at specific analytical angles (Øact: 20, 30, 45, 60, 75, 90, 100 degrees). These simple calculations were made for varying search radiuses (5 – 25%) at different resolutions (14 – 224 pixels/diameter).
What I would want to do is to plot the angular difference (for each specified angle) as a function of both search radius and resolution. i.e.
Øact – Øcalc = f(search radius, resolution)
I am told that I could do this with contourf(), but I am inundated as to how to apply the function to my data as i am not sure how to relate my Z to the X and Y grids. In any case, I have tried the following code:
SR = [5, 10, 15, 20, 25];
res = [14, 28, 56, 112, 224];
angle_diff = [70, 25, 25, 11, 6.6
25, 25, 11, 6.6, 6.6
43.4, 25, 15.5, 11, 10.4
25, 25, 17.9, 12.3, 12.3
36.3, 25, 19.3, 16.5, 14.8];
x = linspace(min(SR), max(SR), 5);
y = linspace(min(res), max(res), 5);
[X, Y] = meshgrid(x, y);
% not sure what to do here
[C, h] = contourf(X, Y, angle_diff);
clabel(C,h)
However, I am not sure what to do in between the meshgrid() and contourf() so as to bring in the values of angle_diff
Please, I need any help/suggestions/reference/advice as to how I could get around this.
An excel spreadsheet of my data for all other angles will be similar to the the figure attached:
Many thanks in advance.
Now i had to pretty much enter the values manually to get something out of this. Hence, I won't mind if there is a better suggestion.
% for angle 20 deg.
X = [5 5 5 5 5 % search radius
10 10 10 10 10
15 15 15 15 15
20 20 20 20 20
25 25 25 25 25];
Y = [14 28 56 112 224 % Resolution
14 28 56 112 224
14 28 56 112 224
14 28 56 112 224
14 28 56 112 224];
Z = [70 25 25 11 6.6 % theta(act) - theta(calc)
25 25 11 6.6 6.6
43.5 25 15.5 11 10.4
25 25 17.9 12.3 12.3
36.3 25 19.3 16.5 14.8];
[C, h] = contourf(X, Y, Z, 20);
clabel(C,h)
title('For analytical angle = 20\circ')
c = colorbar;
c.Label.String = '\thetaact - \thetacalc [\circ]';
c.Label.FontSize = 11;
xlabel('Search radius [% droplet diameter]', 'fontsize',11)
ylabel('Resolution [pixel/diameter]', 'fontsize',11)

I have a matrix 12*4, and I need to subtract the 3rd column elements of rows that are different

I have a 12x4 matrix in MATLAB,
A =[-1, 3, 152, 41.5 ;
3, 9, 152, 38.7 ;
9, 16, 152, 38.7 ;
16, 23, 129, 53.5 ;
23, 29, 129, 53.5 ;
29, 30, 100, 100 ;
30, 30.5, 83, 83 ;
30.5, 31, 83, 83 ;
31, 35, 83, 83 ;
35, 41, 129, 53.5 ;
41, 48, 129, 53.5 ;
48, 55, 152, 38.7 ] ;
and I need to find the changes in the rows by subtracting the 3rd column element of the 2nd row from the previous row 3rd column element if they are different else go to the 3rd row if the same.
The answer should be in the form:
B = [16, 23;
29, 29;
30, 17;
35, 46;
48, 23]
For example, the 3rd and the 4th row 3rd column elements are different, so if subtracted i got 23. Output B 1st column element will consist of the 4th row first column element.
%Given matrix
A =[-1, 3, 152, 41.5 ;
3, 9, 152, 38.7 ;
9, 16, 152, 38.7 ;
16, 23, 129, 53.5 ;
23, 29, 129, 53.5 ;
29, 30, 100, 100 ;
30, 30.5, 83, 83 ;
30.5, 31, 83, 83 ;
31, 35, 83, 83 ;
35, 41, 129, 53.5 ;
41, 48, 129, 53.5 ;
48, 55, 152, 38.7 ] ;
B=A(:,2:3); %Taking out the columns of our interest
B = B([diff(B(:,2))~=0; true],:); %Storing only those rows whose consecutive elements in the third column of A are different
B=[B(1:end-1,1) abs(diff(B(:,2)))] % First column is according to your condition and second column is the difference

numpy: random select K items from total M(M>K) items?

Is there some handy implementation of Matlab function randperm in numpy that random select K items from totally M(M>K) items, and return the selected indice?
In Matlab,
randperm(100,10)
ans =
82 90 13 89 61 10 27 51 97 88
Yes, with the numpy.random.choice function.
>>> numpy.random.choice(100, 10, replace=False)
array([89, 99, 27, 39, 80, 31, 6, 0, 40, 93])
Note that the resulting range is 0 to M-1. If you need 1 to M like MATLAB, add 1 to the result:
>>> numpy.random.choice(100, 10, replace=False) + 1
array([ 28, 23, 15, 90, 18, 65, 86, 100, 99, 1])