matrix indexing to make new matrix - arrays

I am trying to use imported data from a Scanning Transmission X-Ray Microscope raster scan in Matlab to create an intensity image in the form of an array. That is, the y-poisition is held constant and all x values are scanned and an intensity value is given for the relative (x,y) position. Then the next y value is taken, held constant and all x values are scanned, etc. The data is read as the column vectors into Matlab is as follows:
x = x-position ranging in values from 0-326 [104640x1 double]
y = y-position ranging in values from 0-319 [104640x1 double]
I = intensity at position (x,y) [104640x1 double]
I wish to create a a 326x319 array with entries corresponding to intensity vaules at the point (m0,n0)=(x0,y0)
I believe there is an easy way to do this using either matrix indexing or a for statement but I am relatively new to Matlab.

If I understand correctly:
result = reshape(I,327,320);

reshape() function on co-ordinates with respect to intensity should work.

Related

Interpolate 2D Array to single point in MATLAB

I have 3 graphs of an IV curve (monotonic increasing function. consider a positive quadratic function in the 1st quadrant. Photo attached.) at 3 different temperatures that are not obtained linearly. That is, one is obtained at 25C, one at 125C and one at 150C.
What I want to make is an interpolated 2D array to fill in the other temperatures. My current method to build a meshgrid-type array is as follows:
H = 5;
W = 6;
[Wmat,Hmat] = meshgrid(1:W,1:H);
X = [1:W; 1:W];
Y = [ones(1,W); H*ones(1,W)];
Z = [vecsatIE25; vecsatIE125];
img = griddata(X,Y,Z,Wmat,Hmat,'linear')
This works to build a 6x6 array, which I can then index one row from, then interpolate from that 1D array.
This is really not what I want to do.
For example, the rows are # temps = 25C, 50C, 75C, 100C, 125C and 150C. So I must select a temperature of, say, 50C when my temperature is actually 57.5C. Then I can interpolate my I to get my V output. So again for example, my I is 113.2A, and I can actually interpolate a value and get a V for 113.2A.
When I take the attached photo and digitize the plot information, I get an array of points. So my goal is to input any Temperature and any current to get a voltage by interpolation. The type of interpolation is not as important, so long as it produces reasonable values - I do not want nearest neighbor interpolation, linear or something similar is preferred. If it is an option, I will try different kinds of interpolation later (cubic, linear).
I am not sure how I can accomplish this, ideally. The meshgrid array does not need to exist. I simply need the 1 value.
Thank you.
If I understand the question properly, I think what you're looking for is interp2:
Vq = interp2(X,Y,V,Xq,Yq) where Vq is the V you want, Xq and Yq are the temperature and current, and X, Y, and V are the input arrays for temperature, current, and voltage.
As an option, you can change method between 'linear', 'nearest', 'cubic', 'makima', and 'spline'

how to plot 2D intensity plot in matplotlib?

I have an Nx3 array which stores the values in N coordinates. The first and second column correspond to x and y coordinate respectively, and the third column represents the value at that coordinates. I want to plot a 2D intensity plot, what's the best way to do it?
If the coordinates are evenly spaced, then I can use meshgrid and then use imshow, but in my data the coordinates are not evenly spaced. Besides, the array is very large N~100000, and the values (third column) span several orders of magnitude (so I should be using a logplot?). What's the best way to plot such a graph?
You can use griddata to interpolate your data at all 100000 points to a uniform grid (say 100 x 100) and then plot everything with a Log scaling of the colours,
x = data[:,0]
y = data[:,1]
z = data[:,2]
# define grid.
xi = np.linspace(np.min(x),np.max(x),100)
yi = np.linspace(np.min(y),np.max(y),100)
# grid the data.
zi = griddata(x,y,z,xi,yi,interp='linear')
#pcolormesh of interpolated uniform grid with log colormap
plt.pcolormesh(xi,yi,zi,norm=matplotlib.colors.LogNorm())
plt.colormap()
plt.show()
I've not tested this but basic idea should be correct. This has the advantage that you don't need to know your original (large) dataset and can work simply with the grid data xi, yi and zi.
The alternative is to colour a scatterplot,
plt.scatter(x, y, c=z,edgecolors='none', norm=matplotlib.colors.LogNorm())
and turn off the outer edges of the points so they make up a continuous picture.

MatLab: Create an 3D array out of x,y,z coordinates and corresponding intensity values

I have a large 2D-array (31100 x 4) built up like this:
Intensity | X | Y | Z
out of this array I would like to create a 3D-array to visualize it as a 3D-image.
I would somehow have to transform the 2D into a 3D-array. Since there will be plenty of points where there is no intensity value available all of these points should be zero values.
Side information:
min(x) = -152.0120;
max(x) = 161.4350;
min(y) = -256.2560;
max (y) = -52.3801;
min(z) = -428.5920;
max (z) = -152.4210;
Because of the larger number of decimal places I might have to round up. Yet, this would possibly lead to several intensities being assigned to the same coordinate combination. Logically, I would have to add up the intensities in these cases.
This makes everything really complicated.
I would be happy if someone could teach me how to transform a 2D into a 3D array with zero-values for spots without intensity values.
Thanks for reading.

Matlab access vectors from a multi-dimensional array

I have a 4D matrix of size 300x200x3x20 where 300x200 is the size of one video frame, 3 is the number of channels (Red-Green-Blue channels) and 20 is the number of frames.
I want to extract all the color vectors from this matrix and store them in a 2D array of size 3x1,200,000 (300 x 200 x 20 = 1,200,000) where each row represents a component of the RGB color space and each column contain the RGB values of one pixel in the original matrix.
Besides, I want to carry out pixel-wise operations on this data such as extracting visual features but I cannot find a way to effectively access vectors along the third dimension.
How could I efficiently do these, possible without using loops?
Try this code -
IN = your_4D_data;
OUT = reshape(permute(IN,[3 1 2 4]),3,numel(IN)/3);
help reshape says:
B = reshape(A,m,n,p,...) or B = reshape(A,[m n p ...]) returns an n-dimensional array with the same elements as A but reshaped to have the size m-by-n-by-p-by-.... The product of the specified dimensions, m*n*p*..., must be the same as numel(A).
is this what you are looking for?
also, you can adress pixels like this: Matrix(i,j,:,k) which gives you the 3 colorchanels of pixel i,j in frame k.

How to create a numeric vector which gives a uniform grid for all dimensions of a matrix X?

I'm applying the function histcnd to a matrix of size 744x2. This function calculates frequencies of values within certain edges. I want to set the edges to, for example, groups of 5 values, but I can't seem to be able to do it.
The syntax of the function is histcnd(X,edges), where edges must have the same length to the number of columns of X. How do I define 'edges' as a 2-column vector, so that it will group values of each column every 5 values?
What about using something like this:
X = randn(744,2);
[a,b] = size(X);
edges = num2cell([linspace(min(X(1,:)),max(X(1,:)),a/5); linspace(min(X(2,:)), max(X(2,:)),a/5)],2);
# not sure if it's the same histcnd, the one I found wants edges to be a cell array
H = histcnd(X, edges);
You can probably pick the min/max values for each axis in a more intelligent way if you know something about your data.

Resources