How to prevent array form combining

5 次查看(过去 30 天)
I have an array in which each value is the variable of another array. My issue is that the arrays inside of the main keep combining into a single array and I need them to remain seperate. This is the problem:
Sub arrays: A1 = [1 2 3] A2 = [4 5 6] A3 = [7 8 9]
Main Array: A = [A1 A2 A3] = [1 2 3 4 5 6 7 8 9]
The issue is, I need to be able to use A1, A2, and A3 as individual array. When I use A(1) to denote the first array A1 = [1 2 3], the main array only gives the number 1 as it has combine all the array and gives the first value in the combination. I know this can be done by using A(1:3), but this will not work since the program contains hundreds of data sets. If anyone can tell me how to maintain the integrity of my array, with an example preferably, it would be much appreciated

采纳的回答

Walter Roberson
Walter Roberson 2021-1-23
A1 = [1 2 3]; A2 = [4 5 6]; A3 = [7 8 9];
A = {A1, A2, A3}
A = 1x3 cell array
{1×3 double} {1×3 double} {1×3 double}
A{1}
ans = 1×3
1 2 3
Notice the use of {} for indexing to get to the content of the first "cell".
Note that you will not be able to do mathematics directly on A. For example, you will not be able to do something like
A = A + 1;
Instead you would need something like
A = cellfun(@(C) C+1, A, 'uniform', 0);
  1 个评论
Andrew Tubbs
Andrew Tubbs 2021-1-23
Thanks for the help, that supports the array integrity. I'mk not real sure about the cellfun(), but using the notation A{ i } allows me to do inner/extra cell combinations with A{ i } + B{ i }.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by