get a 2d matrix out of a 3d matrix Matlab - matlab

I have a 3d mxnxt matrix , I want to be able to extract t 2d nxm matrices.
In my case I have a 1024x1024x10 matrix and I want to have 10 images showing it to me.
This is not reshaping, I want just part of the data each time, is there a way doing it simpler that just coping member by member the data needed?

Yes, just do e.g.:
my_2d_array = my_3d_array(:,:,n);
For more info, see e.g. http://www.mathworks.com/help/techdoc/math/f1-86528.html.

as Oliver said you can use:
my_2d_array = my_3d_array(:,:,n);
You can use squeeze function to remove the 1*1 of the matrix.

Related

Matlab: reshape 4-d matrix to 2-d and maintain order, how to?

I'm trying yo implement vlsh with the California ND Datastet, wich is composed by 701 photos.
10 subject wrote down in a txt file which photos are near duplicate for them, and we have also correlation matrix.
The images are RGB and i reduced them in 20x20. I created a 4-d array 20x20x3x701. So i tried to reshape and obtained a 1200x701 matrix, but the problem is that reshape can't maintain the order of the original matrix.
I tried to search online and most of suggestion is to use "Permute", but it seems to me that doesn't fit my situation.
I can post the matlab code:
`
path='C:\Users\franc\Desktop\stage\californiaND\prova*.jpg';
path2='C:\Users\franc\Desktop\stage\californiaND\prova';
d=dir(path);
a=[];
for m=1:length(d)
a=cat(4,a,imread(strcat(path2,d(m).name)));
end
r=reshape(a,[],701);
r=double(r);
L = lshConstruct( r, 10,4);`
I am assuming you need the 1D vector in RGBRGB format, as otherwise the solution would be trivial ( just (:)).
Assuming imread is reading 20x20x3 images one by one, this is how you directly make your 2D matrix:
for m=1:length(d) % this is 701, right?
im=imread(strcat(path2,d(m).name));
im=permute(im,[3 1 2]);
a=cat(2,im(:));
end

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");

Plotting from 3D matrix in Matlab

I have a matrix which is 1*1*10000, the slightly odd dimensions are the result of the matrix algebra used to calculate it.
I simply want to be able to plot the 10000 data points contained in it, but matlab seems unable to do it?
Can someone please tell me how I can plot the data?
Seems simple but I really can't figure out how to do it!
Baz
yes you need to reduce the dimensions to a vector:
A = zeros(1,1,100)
vector = squeeze(A(1,1,:))
as when you'd access the third dimension this would only return a 3D-Matrix again:
z = A(1,1,:)
would NOT work. So use squeeze() ;-) Then plot as usual.
Doc-Link: http://www.mathworks.de/de/help/matlab/ref/squeeze.html
And as Ander pointed out in comments, no need to give any dimensions, as it removes singleton-dimensions by itself. So just use vector = squeeze(A). MATLAB recognizes the way to go itself.

How do I take a bunch of 2d matrices from .dat files and put them into one big 3d matrix?

I'm trying to combine all these 2d matrices I have in .dat files into a single 3d matrix.
So what I've done so far is this:
for (i=1:40) //There are 40 of these files
fileName = ['Solutionm1.dat/Solution' num2str(i-1) '.dat'] //This line sets a file name
f=fopen(fileName,'r') //I'm just opening the file now
A{i}=fread(f,'double') //Now I'm converting the binary file into a matlab matrix
B{i}=reshape(A{i},41,21) //Now I'm putting it into the shape that I want (I don't know of a better way to read binary files)
fclose all
end
Ultimately, I want to take the L2 norm of this 3d matrix by using norm((insert 3d matrix here),2)
The problem I'm having is that I just don't know how to combine all the matrices I just made into one big 3D matrix.
Solution
Use
T(:,:,i)=B{i}
or use
T=cat(3,B{:})
Continued problem
This doesn't work now:
norm(T,2) //Should compute the L2 norm of my 3D matrix, right?
This might be out of the scope of this question though...
From what I've learned, I think norm has to be used on a 2D matrix.
Here's the answer!
T=Cat(3,B{:}) //Combines all matrices into one single 3D matrix
Once you have B, run:
matrix3d = zeros(41,21,40);
for i=1:40
matrix3d(:, :, i)=B{i};
end
You can also include matrix3d(:, :, i)=B{i}; in your loop, and call matrix3d = zeros(41,21,40); before entering the loop.

Matlab Isolating 2D Array from 3D Matrix

I have a 3D matrix called M of size <100x100x100>, so basically coordinates.
I am trying to get the array of at specific values of y. However using M(:,1,:) I get a <100x1x100> matrix whereas finding I can use M(:,:,1) and get a <100x100> matrix.
Is there an easy way to turn the <100x1x100> into a <100x100> by either isolating it a different way or using a short translation?
Thanks,
Does squeeze do what you want?
a = ones(100, 1, 100);
b = squeeze(a);
size(b) % 100x100