Assign multiple elements of one cell array to another

42 visualizaciones (últimos 30 días)
Leo Simon
Leo Simon el 19 de Oct. de 2020
Comentada: Walter Roberson el 10 de Ag. de 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}

Respuesta aceptada

Walter Roberson
Walter Roberson el 19 de Oct. de 2020
B(:,:,2) = A;
  8 comentarios
Stephen23
Stephen23 el 10 de Ag. de 2023
Editada: Stephen23 el 10 de Ag. de 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
Walter Roberson el 10 de Ag. de 2023
Burrata cheese has semi-liquid milk stored within a container made out of firmer milk.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by