Scatter non-square matrix - matlab

I would like to scatter matrix of any dimensions (height, width, depth). The problem is that scatter takes matrices of equal dimensions.
Is there any alternative way to achieve this?
I was thinking about complementing my matrix to square one and then minimizing the size and color of those points that are "virtual", but I'm trying to find a better solution.
Can anyone help?
Here is the way I intend this to work:
[x,y,z] = meshgrid(1:height, 1:widht, 1:depth);
scatter3(x(:), y(:), z(:), size, data_matrix(:));
To show purpose of this code: Say, I have temperature in a room stored in a matrix. I want to plot the temperature in a 3D plot. If there is a better way to achieve this then scatter (which seems to work for equal dimensions case), will be grateful to know it :D

Related

Showing Covariance in a 3D graph

I have a 3D scatter plot and I want to visually show COVARIANCE on it. One can show COVARIANCE, for example with an ISO LINE. With this method, one generally gets an ellipse aligned with the shape of the scatter plot. Do you know how I can do this with MATLAB or any other method.
Thanks
I don't understand how would you like to display covariance on a 3D plot. I think what you are looking for is pca , it would give you the three vectors corresponding to maximum variance in your 3D scatter plot. You can then determine the variance along each of those vectors and plot an ellipsoid which represents sort of a confidence region. The final figure would something like this:
There is a little bit of Linear algebra and rotation matrices knowledge involved with this approach.

finding gradient/curvature of surface defined by arbitrary, non-grid-spaced points

So I have a 3 dimensional matrix of points that (presumably) define a surface. For my purposes, X and Y can be random values but when plotted along with their Z coordinates, they will define some undulating surface. I'd like to measure the local curvatures of said surface, and in order to do that, I need to be able to find the gradient of said surface, at which point calculating the curvature is trivial.
I have not yet found an implementation of how to measure this curvature that doesn't make use of Matlab's gradient function. The problem with Matlab's gradient function is that it assumes that the points are in some sort of order, similar to diff(X). This would suffice if my points were spaced along a grid, which is not necessarily the case.
One possible solution to measuring the gradient is to give in and assign each point to a discrete coordinate in a grid in the XY plane, thus overcoming this issue. However, this solution seems somewhat inelegant and was curious to see if anyone had suggestions. Thanks!
You can use griddata to interpolate from your scattered data points to grid spaced points and then calculate the gradient.

Plotting a best fit curves

Suppose I have a best fit equation for a set of data.How can I plot the data with marker and the best fit curve together. I know the command to include a data marker for a curve but this will make the curve pass through all the points if I type something like this
plot(x,polyval(p,x),'kx',x,polyval(p,x))
But this is not what I want since I am plotting a best fit curve. So I would like to know how exactly I can do that. Thanks in advance for all the helps. And between how can I add a polynomial that pass through all the point and the best fit curve as well as the data point with data marker on a same figure.
Where is the data stored? lets say your datapoints are in x and y, then make a much finer xi so something like linspace(min(x), max(x), 1000) or xi = -100:0.01:100 or whatevers appropriate and then to plot
plot(x, y, 'k*', xi, polyval(p, xi))

Matlab 3d plot of indexed data

I am trying to plot a 3d view of a very large CT dataset. My data is in a 3d matrix of 2000x2000x1000 dimension. The object is surrounded by air, which is set to NaN in my matrix.
I would like to be able to see the greyscale value of the surface of the object (no isosurface) but I cannot quite work out how to do that in Matlab. Can anyone help me please?
Given that I a dealing with a huge matrix and I am only interested in the surface of the object, does anyone know a good trick how to reduce the size of my dataset?
The function surf(X,Y,Z) allows you to plot 3d data, where (X,Y) gives the coordinates in the x-y-plane while Z gives the z-coordinate and the surface color.
By default the function does not plot anything for the NaN entries, so you should be good to go with the surf function.
To set the surf-function to use a grayscale plotting use:
surf(matrix3d);
colormap(gray);
This plots the matrix in a surface plot and sets the colormap to grayscale.
In addition, as I understand your data, you might be able to eliminate entire plane-segments in your matrix. If for instance the plane A(1,1:2000,1:1000) is NaN in all entries you could eliminate all those entries (thus the entire Y,Z-plane in entry X=1). This will however require some heavy for loops, which might be over the top. This depends on how many data matrices you have compared to how many different plot you want for each matrix.
I will try to give you some ideas. I assume lack of a direct 3D "surface detector".
Since you have a 3D matrix where XY-planes are CT scan slices and each slice is an image, I would try to find edges of each slice say with edge. This would require some preprocessing like first thresholding each slice image. Then I can either use scatter3 to display the edge data as a 3D point cloud or delaunay3 to display the edge data as a surface.
I hope this will help you achieve what you are asking for.
I managed to get it working:
function [X,Y,Z,C] = extract_surface(file_name,slice_number,voxel_size)
LT = imread(file_name);%..READ THE 2D MAP
BW = im2bw(LT,1);%..THRESHOLD TO BINARY
B = bwboundaries(BW,8,'noholes');%..FIND THE OUTLINE OF THE IMAGE
X = B{1}(:,1);%..EXTRACT X AND Y COORDINATES
Y = B{1}(:,2);
indices = sub2ind(size(LT),X,Y);%..FIND THE CORRESPONDING LINEAR INDICES
C = LT(indices);%..NOW READ THE VALUES AT THE OUTLINE POSITION
Z = ones(size(X))*slice_number;
I can then plot this with
figure
scatter3(X,Y,Z,2,C)
Now the only thing I could improve is to have all these points in the scatter plot connected with a surface. #upperBound you suggested delaunay3 for this purpose - I cannot quite figure out how to do this. Do you have a tip?

3D scatterplot colored by Z-Value

I've been googling for a while but couldn't find a solution for my problem. I am an amateur matlab user and I would like to create a 3D scatterplot, for this I have a matrix containing several points in 3D space:
>> size(A)
ans =
2511 3
I was able to create a 3D scatterplot using "scatter3" function, but now I am stuck a bit at color-coding the 3D points.
scatter3(A(:,1),A(:,2),A(:,3));
This will plot the data, but now I would like to add a color coding based on the z-Value...
The colors themself don't matter too much. It could be a rainbow spectrum or a temperature spectrum or whatever. I just would like to colorcode them to distinguish the z-Values of the points.
Can anybody help me with this? Thank you!
You have to give some more arguments to scatter3.
scatter3(X,Y,Z,S,C);
S lets you specify areas for each markers (with a vector) or a single area for all the markers, while C lets you specify color. If C is a vector, its values will be linearly mapped to the current colormap. To change the colormap, call colormap(jet) for example. See the documentation on colormap.
Sorry if that's confusing. Short version:
scatter3(A(:,1),A(:,2),A(:,3),9,A(:,3));
colormap(jet); %# or other colormap