Accessing specific values in every matrix in a cell array

15 views (last 30 days)
I am using the 2021a release of Matlab if that makes a difference.
Lets say I have a cell array that contains N rows and M columns and each cell entry contains a 1x3 matrix of cartesian coordinates where the z values are in the third position of each matrix. I want to get all the z-values in each cell and put them into a matrix without using loops. I was wondering if there is a prewritten function that pulls out an entry or range of entries inside every matrix in every cell.
I think the equivalent in python would be something like:
MatrixofValues = MyCell[:,:][2]

Accepted Answer

Matt J
Matt J on 3 Jul 2021
Edited: Matt J on 3 Jul 2021
I want to get all the z-values in each cell and put them into a matrix without using loops.
Even with built-in Matlab functions, cell arrays are always processed with the equivalent of an MCoded for-loop. There's no improving upon the efficiency of a for-loop when you're working with cells.That's why you may want to consider storing your data instead as an NxMx3 numeric array.
However, you can avoid the syntax of a for-loop by doing:
MyCell=reshape( num2cell(rand(20,3),2) ,5,4) %Example input
MyCell = 5×4 cell array
{[0.3599 0.0614 0.9934]} {[0.0242 0.2512 0.8680]} {[0.0870 0.5513 0.4783]} {[0.1048 0.8745 0.4824]} {[0.8635 0.5613 0.8529]} {[0.2953 0.6760 0.5563]} {[0.1096 0.7115 0.3442]} {[0.7195 0.6510 0.3524]} {[0.4841 0.2202 0.6542]} {[0.4437 0.3451 0.6559]} {[0.7295 0.5343 0.5754]} {[0.5176 0.1906 0.9097]} {[0.1577 0.4450 0.7309]} {[0.8411 0.9303 0.5594]} {[0.8659 0.5668 0.0578]} {[0.0914 0.7965 0.9632]} {[0.3659 0.4313 0.6224]} {[0.3684 0.3809 0.9393]} {[0.3347 0.8684 0.9387]} {[0.6600 0.8319 0.0485]}
Q=vertcat(MyCell{:});
Matrix=reshape(Q(:,3),size(MyCell))
Matrix = 5×4
0.9934 0.8680 0.4783 0.4824 0.8529 0.5563 0.3442 0.3524 0.6542 0.6559 0.5754 0.9097 0.7309 0.5594 0.0578 0.9632 0.6224 0.9393 0.9387 0.0485
  1 Comment
Richard Miller
Richard Miller on 3 Jul 2021
Edited: Richard Miller on 3 Jul 2021
Thanks, I think I'll just stick with the for loop situation I have because the data I am working with is part of a bigger set of data including area values, area tensors, magnitudes of the tensors in different directions and so forth, so I store the data in a cell array because it's easier for me to visual what my output will look like. The number of data points aren't too big so sacrificing a little efficiency for the ability to visualize what the output will look like and how to handle it in the next function is something I am willing to do. I was just looking to write fewer lines of code and that's what you gave me.

Sign in to comment.

More Answers (2)

Peter O
Peter O on 3 Jul 2021
Sure, use cellfun:
A = {[1,2,3],[4,5,6],[7,8,9];[10,11,12],[13,14,15],[16,17,18]};
B = cellfun(@(x) x(3), A)
B = 2×3
3 6 9 12 15 18
disp(B)
3 6 9 12 15 18

dpb
dpb on 3 Jul 2021
Edited: dpb on 3 Jul 2021
Unfortunately, MATLAB can't do partial array combo addressing -- best I can think of here creates the array xyz as an intermediary and then selects the z column -- which, if the data are as you describe, there's no advantage to having it as a cell array that I see (at least in MATLAB; I "know nuthink'!" about Python for why one would do so there...
xyz=cell2mat(C);
z=xyz(:,3:3:end);
If you just store xyz from the git-go, however, the coordinates are/will be directly available by straight indexing and you likely could do without creating the specific arrays at all.
  1 Comment
dpb
dpb on 3 Jul 2021
NB: The above produces an Nx(3M) array; another alternative would be as Matt J suggests, store by planes in 3D array.
While the cell array is compact at the top level cell arrays have two disadvantages --
  1. Overhead memory -- there's an extra "bunch o' stuff" needed to address the cell arrays that is added memory, and
  2. The dereferencing of the cell array is more complicated syntax and may require temporaries.
In general unless the data are of disparate types or sizes, it's better/more efficient to use native arrays.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!