How to prevent array form combining

조회 수: 3 (최근 30일)
Andrew Tubbs
Andrew Tubbs 2021년 1월 23일
댓글: Andrew Tubbs 2021년 1월 23일
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에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by