Assign multiple elements of one cell array to another

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}

 采纳的回答

B(:,:,2) = A;

8 个评论

Thanks, Walter, now I feel really stupid!!
When to use {} vs. () is very confusing to a lot of people. It gets me all the time.
@Captain Karnage 1000000% correct.
Why is it opposite for assignment vs extraction???
cellArr = cell(N,5);
cellArr(3,:) = {doubleArr1, ... , doubleArr5};
[doubleArr1, ... , doubleArr5] = cellArr{3,:};
@Tyler see the FAQ for understanding when to use braces, parentheses, and brackets:
"Why is it opposite for assignment vs extraction???"
It isn't. You think it is is because you are comparing apples with oranges.
Cell array indexing is very simple: if you want to refer to the array itself, you use parentheses (just like you would for every other array type in MATLAB, there is absolutely no difference). Because cell arrays are a container type, you can also use curly braces to refer to the cell content. Summary:
  • {} refer to the cell array content
  • () refer to the cell array itself.
Cell arrays are containers, you can put things into them and take them out again. And just like containers you can either hold the container in your hand or get content out and hold that. Two totally different actions, which you are confusing: if I ask you for a bottle of milk, would you pour the milk onto my hand or give me the bottle to hold? If I ask you to pour milk onto my cereal, would you place the entire bottle in the middle of my breakfast bowl? That is what you are mixing up.
Lets look at your code. You are confusing yourself because what you see are DOUBLEARR1, DOUBLEARR2... etc. But look at what you are really doing with this line:
cellArr(3,:) = {doubleArr1, .. , doubleArr5};
^ ^ create cell array
^ ^ assign to cell array
The RHS is a cell array. The LHS is a cell array. You are allocating a cell array to a cell array, so of course the LHS uses parentheses (just like every other class in MATLAB, when you refer to the arrays themselves). What is inside those cell arrays is completely irrelevant to that operation (but it confused you because you focussed on the cell array content, and not the actual operation involving cell arrays, which is the thing that you are actually allocating).
In contrast here the RHS uses curly brace indexing:
[doubleArr1, .. , doubleArr5] = cellArr{3,:};
Using curly brace indexing is used for placing content into or getting content out of cell arrays. Referring to the content of multiple elements has a special meaning because it returns that multiple content in a comma-separated list:
But in any case, here is some code which does not compare apples with oranges:
C = cell(..);
C{idx} = someArray % allocate cell array content
someArray = C{idx} % obtain cell array content
C(idx) = anotherCell % allocate cell array itself
anotherCell = C(idx) % obtain cell array itself
This example code shows that we refer to the cell content using curly braces (both assigning and obtaining) and refer to the cell arrays themselves using parentheses (both assigning and obtaining). So far every is perfectly consistent and exactly as documented:
Confusing containers with their content is going to cause a very big mess when you try to eat breakfast.
Using Cell Arrays is unique to using other types of data in MATLAB. I get their usefulness, if someone has heterogenous data, they can store it all together in the same cell array without having to define a class. But when you're writing thousands of lines of code, and 95%+ of your variables use one syntax / coding pattern and this one uses a different syntax / coding pattern, it's easy to be stuck in the typical pattern and use the wrong symbol and get confused by the resulting behavior. 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. I'm not saying it should change, the syntax HAS to be different and I understand why... I'm just acknowledging the struggle of using it. If someone codes in other languages, there's nothing really like it that I can think of, making it even more anomalous in their brain.
It may be perfectly consistent as documented, but the behavior is not intuitive. I understand the analogy, but it's obvious when you're staring at a container of milk vs. a symbolic abstraction of a software concept. Also, you can have a cell array within another cell array - you can't have a container of milk inside another container of milk. I mean, if someone wanted to create that, I suppose they could, but it wouldn't be useful in real life. A cell array inside another cell array could be useful, however. I guess more like having a crate on a pallette with a bunch of smaller boxes inside the crate. You have to remove the layers of cardboard, and {} is like the carboard sides surrounding it.
Just trying to give some perspective here, particularly when communicating with someone who may not be well versed in MATLAB. I've been using MATLAB for over 20 years (to varying degrees year by year) and it still trips me up from time to time.
"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!
Burrata cheese has semi-liquid milk stored within a container made out of firmer milk.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Call Python from MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by