Why am I loosing a row and a column in MATLAB slice plot? - matlab

I am trying to plot a 3D volume in MATLAB. I am using the slice command.
a(:,:,1)=[1,2; 3,4];
a(:,:,2)=[5,6; 9,8];
figure;
slice (a,0,0,1);
hold on
slice (a,0,0,2);
The figure I get has just one square (pixel). I am expecting 4 squares. How do I plot this? What am I doing wrong?

Relevant part of documentation:
slice(V,sx,sy,sz) draws slices along the x, y, z directions in the volume V at the points in the vectors sx, sy, and sz. V is an m-by-n-by-p volume array containing data values at the default location X = 1:n, Y = 1:m, Z = 1:p. Each element in the vectors sx, sy, and sz defines a slice plane in the x-, y-, or z-axis direction.
So, your command slice (a,0,0,1); is asking Matlab to produce three slices of the cube [1,2]×[1,2]×[1,2] (colored according to the values of your a array), by the following planes
x=0 plane (shown as empty square since it's outside of the cube)
y=0 plane (same story)
z=1 plane (dark blue square).
You could have avoided the extraneous x=0 and y=0 slices with slice(a,[],[],1). Also,
slice(a,[],[],[1,2]) would give you top and bottom
slice(a,[],[1,2],[]) would give two vertical sides
slice(a,[1,2],[],[]) would give two other vertical sides
Or you could just get all six at once with slice(a,[1,2],[1,2],[1,2]). If you don't want, e.g., top and bottom slices, then slice(a,[1,2],[1,2],[]).
Note that the entries of a are not coordinates, they are understood as values of a function of three variables, and are represented by colors.

Related

matlab heatmap plot changing axis directions

I have a function f(v,z) and z = [0:0.01:1], v = [1:0.01:5].
I first create a cell, denoted by C, to record the coordinates of this (v,z), and from this C, I fill in a matrix of the value of f(v,z). So my cell C look like the following
C = (0,5), (0.01,5), (0.02,5),....., (1,5)
(0,4.99),(0.01,4.99),(0.02,4.99),...,(1,4.99)
...
(0,1), (0.01,1), (0.02,1),......,(1,1)
So when viewing cell C as the coordinates in the 2D space, the vertical axis (the column) is z and the horizontal axis (the row) is v. And the value f(v,z) is built up on these coordinates. So one can imagining plotting a 3D picture.
Then I fill matrix M, which contains the value of f(v,z) according to the cell C. Then I want to do the heat map (or contour would also be fine), but when I use colormap or contour, it seems that matlab just does not plot the figure according to my specific direction of v and z as specified by my cell C. Also, the range of the two axes does not look right. I wonder if there is good way to control the plotting directions of the axes?

Compute multiple faces connected at vertices from normal vectors

I have a set of 8 normal vectors in 3D space. I need to plot a cuboid with lateral wings from these vectors:
whereas vectors 4 to 6 are just the negatives (opposite sides) of the first three and the last two vectors are identical to the first one and make up the wings.
The goal is to plot this object with each surface having a different color so that I can count the pixels of each color afterward for different sets of normal vectors (i.e. determining the visibility of partially obstructed surfaces).
I have found the patch function which can plot polygons with individual colors and takes the coordinates of the vertices for input. However, I do know the dimension of each surface, but the coordinates of the vertices need to be somehow calculated for each set of normals vectors. Any suggestions on how to approach this task?
EDIT
I may have found a solution: I just noticed that view allows defined azimuth and elevation angles and thus "rotation" of the object. With the compution of azimuth and elevation from the normal vectors, the calculation of the vertices becomes unnecessary.

Creating a mask with 3 point in Matlab?

I have this 3 points (x,y) and I need to obtain a mask with a triangle where vertices is the points. I should respect some parameters, like the pixel pitch and I need a grid from the minimum x cordinate to the maximum x coordinate (the same for the y).
I tried to do this in matlab with the function poly2mask but the problem is the resultant image: when I have negative coordinates, I cannot see the polygon.
So I tried to center the polygon but I loose the original coordinates and I cannot have they back again because I need to do some elaboration on the image.
How I can obtain a mask triangle from 3 points without modifying the points and respecting the parameters?

MATLAB 3D volume visualization

I have a matrix M, 135*191*121 double and want to plot it in 3D volume by using those 121 slices. How can I do this? (I need a grayscale image)
Regards
Check out vol3d v2 , it an update to Joe Conti's vol3d function, allowing voxel colors and alpha values to be defined explicitly. In cases where voxels can be any RGB color, use:
vol3d('CData', cdata);
where cdata is an MxNxPx3 array, with RGB color along the 4th dimension. In cases where color and alpha values are highly independent, specify an MxNxP alphamatte as follows:
vol3d('CData', cdata, 'Alpha', alpha);
if you have 3 arrays, storing (x,y,z) coordinates of every point that you need to plot, then you can use function plot3
From matlab help
PLOT3 Plot lines and points in 3-D space.
PLOT3() is a three-dimensional analogue of PLOT().
PLOT3(x,y,z), where x, y and z are three vectors of the same length,
plots a line in 3-space through the points whose coordinates are the
elements of x, y and z.
PLOT3(X,Y,Z), where X, Y and Z are three matrices of the same size,
plots several lines obtained from the columns of X, Y and Z.
Various line types, plot symbols and colors may be obtained with
PLOT3(X,Y,Z,s) where s is a 1, 2 or 3 character string made from
the characters listed under the PLOT command.
PLOT3(x1,y1,z1,s1,x2,y2,z2,s2,x3,y3,z3,s3,...) combines the plots
defined by the (x,y,z,s) fourtuples, where the x's, y's and z's are
vectors or matrices and the s's are strings.
Example: A helix:
t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t);
PLOT3 returns a column vector of handles to lineseries objects, one
handle per line. The X,Y,Z triples, or X,Y,Z,S quads, can be
followed by parameter/value pairs to specify additional
properties of the lines.
for 3d plots you may also want to look into surf function

Find closest point in matlab grid

G'day
I'm trying to program a smart way to find the closest grid points to the points along a contour.
The grid is a 2-dimensional grid, stored in x and y (which contain the x and y kilometre positions of the grid cells).
The contour is a line, made up of x and y locations, not necessarily regularly spaced.
This is shown below - the red dots are the grid, and the blue dots are the points on the contour. How do I find the indices of the red dot closest to each blue dot?
Edit - I should mention that the grid is a latitude/longitude grid, of an area fairly close to the south pole. So, the points (the red dots) are the position in metres from the south pole (using a polar stereographic representation). Since the grid is a geographic grid there is unequal grid spacing - with slightly different shaped cells (where the red dots define the vertices of the cells) due to the distortion at high latitudes.
The result is that I can't just find which row/column of the x and y matrix corresponds closest to the input point coordinates - unlike a regular grid from meshgrid, the values in the rows and columns vary...
Cheers
Dave
The usual method is to go:
for every blue point {
for every red point {
is this the closest so far
}
}
But a better way is to put the red data into a kd tree. This is a tree that splits the data along its mean, then splits the two data sets along their means etc until you have them separated into a tree structure.
This will change your searching effeciancy from O(n*m) to O(log(n)*m)
Here is a library:
http://www.mathworks.com.au/matlabcentral/fileexchange/4586-k-d-tree
This library will provide you the means to easily make a kd tree out of the data and to search for the closest point in it.
Alternatively you can use a quadtree, not as simple but the same idea. (you may have to write your own library for that)
Make sure the largest data set (in this case your red points) go into the tree as this will provide the greatest time reduction.
I think I've found a way to do it using the nearest flag of griddata.
I make a matrix that is the same size as the grid x and y matrices, but is filled with the linear indices of the corresponding matrix element. This is formed by reshaping a vector (which is 1:size(x,1)*size(x,2)) to the same dimensions as x.
I then use griddata and the nearest flag to find the linear index of the point closest to each point on my contour (blue dots). Then, simply converting back to subscript notation with ind2sub leaves me with a 2 row vectors describing the matrix subscripts for the points closest to each point on the blue-dotted contour.
This plot below shows the contour (blue dots), the grid (red dots) and the closest grid points (green dots).
This is the code snippet I used:
index_matrix1 = 1:size(x,1)*size(x,2);
index_matrix1 = reshape(index_matrix1,size(x));
lin_ind = griddata(x,y,index_matrix1,CX,CY,'nearest'); % where CX and CY are the coords of the contour
[sub_ind(1,:),sub_ind(2,:)] = ind2sub(size(x),lin_ind);
I suppose that in the stereographic representation, your points form a neat grid in r-theta coordinates. (I'm not too familiar with this, so correct me if I'm wrong. My suggestion may still apply).
For plotting you convert from the stereographic to latitude-longitude, which distorts the grid. However, for finding the nearest point, consider converting the latitude-longitude of the blue contour points into stereographic coordinates, where it is easy to determine the cell for each point using its r and theta values.
If you can index the cell in the stereographic representation, the index will be the same when you transform to another representation.
The main requirement is that under some transformation, the grid points are defined by two vectors, X and Y, so that for any x in X and y in Y, (x, y) is a grid point. Next transform both the grid and the contour points by that transformation. Then given an arbitrary point (x1, y1), we can find the appropriate grid cell by finding the closest x to x1 and the closest y to y1. Transform back to get the points in the desired coordinate system.
dsearchn: N-D nearest point search.
[k, d] = dsearchn(A,B) : returns the distances, d, to the closest points. d is a column vector of length p.
http://au.mathworks.com/help/matlab/ref/dsearchn.html?s_tid=gn_loc_drop