How to plot n dimensional matrix? - matlab

I want to visualize my matrix with the computed mean of that matrix.
And I want to plot that points and the mean at the same window.
here is my Matrix and the Mean
Input=4 1 1
3 9 0
2 5 5]
Average=mean(Input
How to plot it??
To plot figure I using this command:
plot(InputMatrix(:,:,:),Average'*');
There are 9 points, but I just need 3 points from matrix and 1 point of the mean...
1st point from -->4 3 2
2nd point from -->1 9 0
3rd point from -->1 0 5
the 4th point is -->the mean / average

You can use plot3:
plot3(Input(1,:),Input(2,:),Input(3,:),'o') %// plot each point (in 3D)
hold on
m = mean(Input,2); %// compute mean of all points
plot3(m(1),m(2),m(3),'*') %// plot the mean

Related

MATLAB: How to add custom ticks and labels to an imagesc plot?

I have made a matrix containing 13 different vectors with ~300K+ rows. I have visualized the matrix by transposing it and using the imagesc function to see the distribution of colors. All vectors have been resampled, processed and normalized between 0 & 1 individually.
The imagesc plot gives me this result (fig 1):
But, when I use the axis functionality to add x & y limits, I get this:
How do I maintain the imagesc plot while being able to add custom ticks and labels to the X & y axis? The x axis represents time, while the y axis will get its own labels with sensor names.
You redefine limits from 0 to 30 on the x-axis while the initial xlimits goes up to 3e5. Same issue with the y-axis
Here's how to redefine the Y-axis to put sensor names:
C = [0 2 4 6 9 ; 8 10 12 44 14; 16 48 10 32 23];
image(C)
% Get axis handle
ax = gca;
% Set where ticks will be
ax.YTick = [1 2 3];
% Set TickLabels;
ax.YTickLabel = {'S1','S2','S3'};
Figure out the ax.YTick where you want the labels to appear.
If you want the x-axis to go from 0 to 30, divide the x component of all vectors by 1e4 before plotting. Alternatively, you can add the line:
ax.XTickLabel = ax.XTick/1e4;

How to label plot having peaks in matlab

I'm trying to label my XRD data which have peaks, and I want to label it from my array of data:
peak label
ab
ac
ad
cb
bb
ba
See picture below
I also want those labels to be vertically aligned on the top of the peaks.
I tried the findpeaks function but it doesn't work.
Try this (but you need to have a Signal Processing Toobox):
x = [1 2 3 4 5 6 7 8 9]
y = [1 4 2 7 3 9 5 10 2]
[peak, peakId] = findpeaks(y); %find peaks in your serie
figure(1)
plot(x, y)
lbalph=('a':'z').'
lb=strcat(Alphabet(1),lbalph(1:length(peak))) %Create a label matrix
lb = num2cell(lb,2) % Convert to cell array
lbid = 1:length(lb)
text(x(peakId), peak, lb(lbid),'Rotation',90) % label the peak with your lb matrix
As you have the index peaks, you can labeled as you want.

Checking values of two vectors against eachother and then using the column location of equal entries to extract colums from a matrix in matlab

I'm doing a curve fitting problem in Matlab and so far I've set up some orthonormal polynomials along a specified range of x-values with x = (0:0.0001:40);
The polynomials themselves are each a manipulation of that x vector and are stored as a row in a matrix. I also have some have data entries in the form of two vectors - one for the data x-coords and one for the actual values. I need a way to use the x-coords of my data points to find the same values in my continuous x-vector and then take the corresponding columns from my polynomial matrix and add them to a new matrix.
EDIT: To be more clear. I have, for example:
x = [0 1 2 3 4 5]
Polynomial =
1 1 1 1 1 1
0 1 2 3 4 5
0 1 4 9 16 25
% Data values:
x-coord = [1 3 4]
values = [5 3 8]
I want to check the x-coord values against 'x' to find the corresponding columns and then pull out those columns from the polynomial matrix to get:
Polynomial =
1 1 1
1 3 4
1 9 16
If your x, Polynomial, and xcoord are the same length you could use logical indexing which is elegant; something along the lines of Polynomial(x==xcoord). But since this doesn't seem to be the case, there's a less fancy solution with a for-loop and find(xcoord(i)==x)

Find "external" elements bigger than a threshold value in a 3D matrix

I was wondering if someone could help me come up with a code for a 3D image I'm working on wright now.
I've got a simple 3D matrix:
A(:,:,1) =
0 7 4
0 32 9
4 3 1
A(:,:,2) =
6 0 4
3 4 6
2 3 11
A(:,:,3) =
12 2 4
10 20 6
14 3 2
I would like to find those values that are bigger than a threshold value (for example biger than 7). However I only want those that are exterior elements, that is, not "central" elements (the 32 on the first layer of the matrix shouldn't be marked as a maximum)
(I'm working with a bigger matrix but I guess that once I'm able to do this for the small 3D matrix from above, it won't be difficult to do it for larger ones).
Thank you a lot
Try this:
A = randn(4,4,4); % data. Arbitrary size
th = 1; % threshold
ind = find(A>th);
[x y z] = ind2sub(size(A), ind);
ext = find((x==1)|(x==size(A,1))|(y==1)|(y==size(A,2))|(z==1)|(z==size(A,3)));
ind_solution = ind(ext); % linear index of desired values
solution = A(ind_solution) % desired values
I'm guessing you could extract vectors from those matrices... so it's a matter of getting the external vectors and looping trough their elements.
I think this link will help you extract a vector.

how to let each element in a vector be plotted in 1 second on the x axis in matlab

I have a vector x=[0 0 0 1 1 2 1 3 1 4] i need to plot it as stairs graph but the space between each element and the another one on the x axis must be 1 unit or 1 second for example , as when the vector elements increase the matlab plot more than one element in the same unit on x axis
so how can i adjust the x axis to plot each element in 1 second on x axis ?