Hello,
I am currently having trouble pulling data from a 4-D array through indexing.
I start with a 3-D array which represents the 3D coordinates of my system. At each point in the 3D matrix I assign a random 3D vector, making it 4-D. I would like to pull the corresponding vector from many different points within the array. The 3D coordinates of the points I want to pull are stored in a 2D array with the columns being the (x,y,z) points.
%% Build array
dim = 40;totalLocations = dim^3;randomVec = rand(totalLocations,3);volume = reshape(randomVec,dim,dim,dim,3);%% Request data from 4d array
vec = [1 1 1];ind = [1:3];vector1 = (volume(vec(:,1),vec(:,2),vec(:,3),ind))
Output:
vector1(:,:,1,1) = 0.4150vector1(:,:,1,2) = 0.0820vector1(:,:,1,3) = 0.0865
The above is an example of how I pull the corresponding vector from the position (1,1,1). Since this is only one point I can squeeze out the extra dimensions and end up with the desired vector. My issue is when I try and expand this to multiple (x,y,z) positions by adding additional rows to vec. For example:
%% Build arraydim = 40;totalLocations = dim^3;randomVec = rand(totalLocations,3);volume = reshape(randomVec,dim,dim,dim,3);%% Request data from 4d arrayvec = [1 1 1;1 1 2];ind = [1:3];clcvector1 = volume(vec(:,1),vec(:,2),vec(:,3),ind)
My output is:
vector1(:,:,1,1) = 0.4150 0.4150 0.4150 0.4150vector1(:,:,2,1) = 0.5320 0.5320 0.5320 0.5320vector1(:,:,1,2) = 0.0820 0.0820 0.0820 0.0820vector1(:,:,2,2) = 0.4980 0.4980 0.4980 0.4980vector1(:,:,1,3) = 0.0865 0.0865 0.0865 0.0865vector1(:,:,2,3) = 0.7612 0.7612 0.7612 0.7612
Which appears to give me the correct numbers but they all appear (number of rows in vec)^2 times. Can anyone help me with how to pull the vectors from many points in the 3-D space through use of a list of (x,y,z) coordinates. I need to do this without loops due to the size of my arrays.
Please let me know if more clarification is needed.
Thanks.
Best Answer