How to prevent array form combining

5 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 23 Jan 2021
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 Comment
Andrew Tubbs
Andrew Tubbs on 23 Jan 2021
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 }.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!