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
Related
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
Is it possible to use pagefun to calculate the norm (and execute other built-in GPU functions - http://www.mathworks.co.uk/help/distcomp/run-built-in-functions-on-a-gpu.html) on multiple pages simultaneously?
For example, I need to calculate the norms of a 3D array.
N = 10000
Sig = gpuArray(2,2000,N) % This is just to get an idea of the dimensions. Its populated elsewhere
% This is what I am currently doing.
for k = 1:N
TNorm(k,:) = [norm(Sig(1,:,k),2) norm(Sig(2,:,k),2)];
end
Is there a way to execute it in one go rather than iterate through the 3rd dimension to calculate the norm each time? Can something like this be done?
pagefun(norm(Sig,2)) % This gives the error: Input data must be a double or single vector or 2D matrix.
Thanks in advance! :)
Say you have a 3D array, in your case lets call it E:
E = rand(3,3,4) % This is a 3D array with size 3x3x4
if you want to calculate the norm of page so from 1 to 4 in this case, you can use:
norm_E = arrayfun(#(idx) norm(E(:,:,idx)), 1:size(E,3))
The output will be:
norm_E = [(norm array 1),(norm array 2),(norm array 3),(norm array 4)]
You are also not limited to the 2-norm you can use any norm you please for example if you wanted to use the infinity norm you could put:
norm_E = arrayfun(#(idx) norm(E(:,:,idx),inf), 1:size(E,3))
Using arrayfun is faster than a for loop. I apologize if you are restricted to using pagefun I do not have that feature with my version of Matlab.
for examples, I have a 6x6 matrix, then I want to take out the small matrix which is located in the center of that matrix, say 2x2. Is there any smart way to do it ? Or I have to loop through the old matrix and then copying values to new one?
Thank you very much.
Of course you can. try for instance
A = rand(6,6); % // big matrix, an example
B = A(3:4,3:4); % // central sub matrix obtained using indices
which (in this case) is also equivalent to
B = A([3 4],[3 4]);
In general you can extract subvectors from a vector selecting the indices you are interested to.
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.
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.