Using MATLAB, is there a way to get a 2D visualisation of data points that are in 6D space? - matlab

Part of an assignment of mine is to provide a 2D visualisation (plots) of some of my data points that are stored in a matrix. I'm slightly confused because the data is actually in 6D space (i.e. each row has 6 columns like 0 1 0 8 8 2).
Is there something I'm missing or does this genuinely not make sense? Is this something MATLAB can do?
Edit: Is something like this possible?

Though I wouldn't consider it visualizing 6D data, you can get the linked plot with a simple call to plot:
A = rand(6);
x = 1:6;
plot(x,A'); % Transpose A to plot rows since it's square, see plot documentation
Which produces the following:
From the documentation:
If one of X or Y is a vector and the other is a matrix, then the
matrix must have dimensions such that one of its dimensions equals the
vector length. If the number of matrix rows equals the vector length,
then the plot function plots each matrix column versus the vector. If
the number of matrix columns equals the vector length, then the
function plots each matrix row versus the vector. If the matrix is
square, then the function plots each column versus the vector.

Simply use:
surf(2Dmatrix)
You can read more here: http://uk.mathworks.com/help/matlab/ref/surf.html
If your matrix is 2D image, simply use
figure; imshow(2Dmatrix, [])
If you leave the square bracket empty, the limit will be automatic. When the figure is displayed you can change it to different colormap by Edit > Colormap.

Related

Matlab: Surf 2d matrix with predefined X-Values

I have a matrix called 'totalAverage' with this size:
size(totalAverage)
ans =
65 110
When I run surf(totalAverage)) it shows a 3d-surf plot.
However I want X-values of surf plot be mapped with special values brought from a matrix called time_matrix_filtered. Currently surf plot is labeled from (1-110) according to matrix size in AXIS-X.
How can I plot the same surf but have X-Values brought from time_matrix_filtered? I tried something like surf(time_matrix_filtered,totalAverage) which works for plot but I don't know the right notation for this special case?
Size of time_matrix_filtered is as below:
size(time_matrix_filtered)
ans =
110 1
You need Y values too:
surf(time_matrix_filtered, 1:65, totalAverage)

Creating 2D points near y=x

I need to generate some random 2D points (for example 30 points) near the y=x line, insert them in a matrix, plot it and then calculate the SVD of the matrix. But since I'm new to MATLAB I don't know how can I generate my desired matrix.
Since this looks like homework I'll just post some general ideas here.
randi can be used to get semi-random integers. Using that you can create a 2D matrix by duplicating the array and putting them together. Thus: generate a 30x1 column and duplicate it to a 30x2 column. All rows will have the same two entries, i.e. x=y.
Noise can be added to this by creating a 30x2 matrix of random numbers, use rand for that and simply add that to the previously created matrix.
Check the documentation on svd to see how the singular-value decomposition works, it's fairly straight-forward if you know your linear algebra.
Finally for plotting you can use various tools such as image, imagesc, plot, surf and scatter, try them and see which works best for you.
Here is a quick example I made: https://saturnapi.com/fullstack/2d-points-randomly-near-line
%// Welcome to Saturn's MATLAB-Octave API.
%// Delete the sample code below these comments and write your own!'
x = 13 + 6.*rand(20,1);
y = x*0.7 + 0.5*rand(20,1);
[X,Y] = meshgrid(x,y)
figure(1);
plot(x,y,'.');
%// Print plot as PNG with resultion of 60 pixels per inch
print("MyPNG.png", "-dpng", "-r60");

Randomly select Elements of 4D matrix in Matlab

I have a 4D matrix with dimensions 7x4x24x10 (Lets call it main_mat). I want to get a matrix of size 7x4x24 (rand_mat) so that each element of this (rand_mat) matrix is actually a uniformly random draw from the main matrix (main_mat). I am sorry if I have not put the question clearly, so I try to explain:
I have a stack of 24 sheets of 7x4 elements, and I have 10 such stacks. What I want is that I get a single stack of 24 sheets of 7x4 elements in such a way that every element out of resultant single stack is uniformly randomly drawn from exactly same sheet number from within 10 stacks. How can I do it without using loops?
If I am interpreting what you want correctly, for each unique 3D position in this matrix of 7 x 4 x 24, you want to be sure that we randomly sample from one out of the 10 stacks that share the same 3D spatial position.
What I would recommend you do is generate random integers that are from 1 to 10 that is of size 7 x 4 x 24 long, then use sub2ind along with ndgrid. You can certainly use randi as you have alluded to in the comments.
We'd use ndgrid to generate a grid of 3D coordinates, then use the random integers we generated to access the fourth dimension. Given the fact that your 4D matrix is stored in A, do something like this:
rnd = randi(size(A,4), size(A,1), size(A,2), size(A,3));
[R,C,D] = ndgrid(1:size(A,1), 1:size(A,2), 1:size(A,3));
ind = sub2ind(size(A), R, C, D, rnd);
B = A(ind);
Bear in mind that the above code will work for any 4D matrix. The first line of code generates a 7 x 4 x 24 matrix of random integers between [1,10]. Next, we generate a 3D grid of spatial coordinates and then use sub2ind to generate column-major indices where we can sample from the matrix A in such a way where each unique 3D spatial location of the matrix A only samples from one chunk and only one chunk. We then use these column-major indices to sample from A to produce our output matrix B.
This problem might not be solvable without the use of loops. One way that could work is:
mainMatrix = ... (7x4x24x10 matrix)
randMatrix = zeros(mainMatrix(:,1,1,1), mainMatrix(1,:,1,1), mainMatrix(1,1,:,1))
for x = 1:length(mainMatrix(:,1,1,1))
for y = 1:length(mainMatrix(1,:,1,1))
for z = 1:length(mainMatrix(1,2,:,1))
randMatrix(x,y,z) = mainMatrix(x,y,z,randi(10))
end
end
end

MATLAB: Need to make a 4D plot (3D + Colour/Color)

I need to make a 3D surface where colour will represent the fourth variable. I know "surf" is SIMILAR to what I need, but that's not quite it. Basically, I have the following variables:
t = [1:m]
y = [1:n]
a = [1:o]
These should be the three Cartesian corodinate axes.
I also have a variable S that is of dimensions m x n x o, and is basically the amplitude, a function of the previous three variables (i.e. S = f(t,y,a)). I want this to be represented by colour.
So to summarize, I need a graph of the form (t,y,a,S), where the first three variables are vectors of unequal sizes and the final variable is a multidimensional array whose dimensions are determined by the first three.
Thanks in advance.
SCATTER3 requires x, y and z and other grouping arguments to be equally-sized Nx1 vectors for a single series or NxM matrices for M series.
You have full space 3D data. To make equally-sized coordinate vectors use MESHGRID (or NDGRID) function:
[X, Y, Z] = meshgrid(t, y, a);
Then you can use SCATTER3:
scatter3( X(:), Y(:), Z(:), [], S(:) )
The problem is since it's full space data scatter3 will not be helpful specially if you have a lot of points.
You can probably filter your S variable (something like idx = S > 0), then you can plot filtered data.
If you really need to visualize all the data, look at Volume visualization in MATLAB documentation. I can recommend SLICE function, for example.
EDIT
Here is an example of full 3D space scatter plot for small vectors (m, n, o equal to 5) with S = rand([m,n,o]); scatter3( X(:), Y(:), Z(:), [], S(:), 'filled' )
EDIT 2
From your comments to the other answer I found that you have 32x76050x4 matrix. You can actually plot 2D slice one at a time. you can do it in 2D with IMAGESC function, or in 3D with SLICE function.
Try:
imagesc(S(:,:,k))
where k is a number from 1 to 4 for the 3rd dimension.
Or try
slice(S, [], [], 1:size(S,3))
shading flat
Maybe this user-created plotting routine can help.
Screnshot from the linked page:
I've always used scatter3 for coloring/sizing pixels in 3d space. I believe the signature is:
scatter3(x,y,z, size, color)
The size and color can be scalar or vector of length equal to the coordinates. I usually use either the color or the size to reflect the fourth attribute, depending on what I'm showing. I don't have Matlab on this machine, so forgive me if my memory isn't completely accurate on the usage. "help scatter3" should describe it much better.

Making a 3D plot of multiple column vectors

I have multiple vectors of varying lengths that I would like to plot next to each other in 3D space in Matlab.
As an example:
Say I have three vectors:
X is a 5x2 vector,
Y is a 10x2 vector and
Z is a 15x2 vector.
Each element of every vector has the format:
x value, y value
but the x values of the various vectors do not match.
I would like to plot these vectors in 3D space, next to each other. The reason why I don't want to plot them using "hold" is because most of the data have the same values, but I would like to see how many of the plots have the same value at a specific time.
I hope my questions makes sense. Please just ask if anyone is unsure.
I think you are looking for the function ribbon.
Documentation: http://www.mathworks.fr/help/techdoc/ref/ribbon.html
EDIT:
if your x's do not have the same length, you can combine it with interp1 as follow:
x1=0:0.1:1;
x2=0:0.02:1.5;
y1=x1.^2;
y2=sqrt(x2);
y2=interp1(x2,y2,x1);
ribbon(x1',[y1;y2]')