Assign multiple elements of one cell array to another
28 views (last 30 days)
Show older comments
Leo Simon
on 19 Oct 2020
Commented: Walter Roberson
on 10 Aug 2023
Suppose I have a cell array A as below and I'd like B{;,:,2} to duplicate A. How can I do this without running a double loop? Presumably using deal but the examples on the web that I've seen don't cover this kind of case.
A =
4x8 cell array
Columns 1 through 7
{1x101 double} {1x101 double} {1x101 double} {1x101 double} {1x101 double} {1x101 double} {1x101 double}
{1x101 double} {1x101 double} {1x101 double} {1x101 double} {1x101 double} {1x101 double} {1x101 double}
{1x101 double} {1x101 double} {1x101 double} {1x101 double} {1x101 double} {1x101 double} {1x101 double}
{1x101 double} {1x101 double} {1x101 double} {1x101 double} {1x101 double} {1x101 double} {1x101 double}
Column 8
{1x101 double}
{1x101 double}
{1x101 double}
{1x101 double}
0 Comments
Accepted Answer
Walter Roberson
on 19 Oct 2020
B(:,:,2) = A;
8 Comments
Stephen23
on 10 Aug 2023
Edited: Stephen23
on 10 Aug 2023
"Using Cell Arrays is unique to using other types of data in MATLAB."
Nope, that is incorrect: actually string arrays behave in exactly the same way, tables differ due to their implicit concatenation after using curly braces, but the same basic rules apply for many MATLAB container types (cell, string, table, other table types..): parentheses refer to the array itself, curly braces to its content. This simple consistency is one of the things that makes MATLAB so easy to work with: index into a table using parentheses and without even looking up the documentation, I can tell you it will return another table. Want its content? Use curly braces. Go ahead and try it yourself.
The main exception to this is structures, which use fields instead of curly braces.
"If someone codes in other languages, there's nothing really like it that I can think of"
Many (most?) modern programming languages have container types:
For example, the most commonly used data types in Python (lists, tuples) are container arrays much like cell arrays (note that Python itself does not have a native type that store contiguous numeric data in memory, i.e. it has nothing like MATLAB's numeric arrays, for which you need to use supplementary modules/libraries e.g. numpy). Both lists and tuples are implemented much like a cell array, as a contiguous array of references to other objects in memory.
Sticking with the Python example: have you ever noticed that if you index into one element of a list it returns the content but if you index into multiple elements it returns another list i.e. the container array itself. Does this inconsistency disturb you? Python's syntax is inconsistent: it changes what indexing does/means, depending on how many elements you index.
"the syntax HAS to be different"
It does not have to be. Many languages seem to be much like Python: they specify an inconsistent indexing meaning to avoid the need to use two different types of indexing operator for their "list"-like containers. Then the poor programmers usually cover their tracks by some kind of smoke-and-mirror definitions e.g. that indexing only refers to one element (i.e. the list content) and that slicing is a completely different thing altogether (that just looks like indexing, quacks like indexing, and waddles like indexing...). Or they use nested lists of nested lists of nested lists of nested lists... which requires indexing next to indexing next to indexing next to indexing... ugh, the horror of it.
"When someone pulls out a cell array (or uses a function that generates a cell array, whether they want it or not) they have to shift mindsets."
Of course they do: containers are a completely different kettle of fish to contiguous homogenous arrays. Understanding is the job of the programmer, whether using MATLAB or Python or any other language: writing Python code assuming that a numpy float32 array can contain heterogenous data is unlikely to work.
"but the behavior is not intuitive"
Nothing is programming is intuitive, it is all learned behavior. Practice, practice, practice.
Good luck!
Walter Roberson
on 10 Aug 2023
Burrata cheese has semi-liquid milk stored within a container made out of firmer milk.
More Answers (0)
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!