How to Extract Part of a Specific Column of elements in a 4D array?

18 views (last 30 days)
I have a 147093 x 1 x 1 x 6 double matrix (originally in the form of a .mgh surface file) that I read into matlab. I need to extract the first 3 values of the 6 column for each of the 147093 indices and put them into a formula (it's just calculating the angle between each vector and a constant <0,1,0> vector). Once I get the angle of every 3D vector I need to store the result in 147093 x 1 array so I can use it in another piece of software. I've attached some pictures for reference. One is the dimensions of the array in matlab, the other photo shows the 3 values for each index that I need to extract, and the other 3 that I need to leave out.
How should I go about doing this? I was thinking about writing a nested for loop, but that doesn't seem very efficient because I know Matlab has a lot of built in functions to do this type of stuff, I'm just not very familiar with them. My main problem is figuring out how to extract the exact values I need. Because I don't even want the other 3 values in the 6 column, it will mess up the calculation.
Thanks for any advice.
  1 Comment
Krishan Bansal
Krishan Bansal on 11 Sep 2022
just as an update, I was able to isolate the array I needed by just highlighting it manually in the place were it shows all the cells. Not ideal, and I'd like a loop or function to be doing it, but for now at least I can see if doing the calculations for each element will work.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 11 Sep 2022
Did you try squeeze?
img = squeeze(m(:, 1, 1, :));
size(img)
imshow(img, []);
  1 Comment
Krishan Bansal
Krishan Bansal on 11 Sep 2022
@Image Analyst I used squeeze and it gave me this matrix, but the problem is that I only need the first 3 values of each index, not all 6. Because for each index I have to take the first 3 values as a single vector, then use it for a calculation, then end up with a single number to store back into a 147093x1 matrix.
Is there a way to isolate just the first 3 values of each row and use them for calculations?

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!