Assign multiple values to a cell

조회 수: 41 (최근 30일)
Riccardo Tronconi
Riccardo Tronconi 2021년 10월 6일
편집: Riccardo Tronconi 2021년 10월 6일
I'm trying to assign three value stored in 2 different array to a single cell.
P=[1;2];
XY=[2,5; 4 ,6];
C represent my cell. The desired output is:
C{1,1}= [1,2,5];
C{2,1}= [2,4,6];
Is there a way to do that?

채택된 답변

C B
C B 2021년 10월 6일
편집: C B 2021년 10월 6일
@Riccardo Tronconi Here you Go !
P=[1;2];
XY=[2,5; 4 ,6];
C= [P(:,:) XY(:,:)]
%OR
C=arrayfun(@(x) [P(x,:) XY(x,:)],P,'UniformOutput',false)
Please Click on "Accept this answer" If its fullfilled your Requirement.
C =
2×1 cell array
{[1 2 5]}
{[2 4 6]}
  댓글 수: 1
Riccardo Tronconi
Riccardo Tronconi 2021년 10월 6일
편집: Riccardo Tronconi 2021년 10월 6일
@Chetan Bhavsar I have a small issue.
when P=[1]
and XY remains the same =[2,5; 4 ,6];
I have this error: Index in position 1 exceeds array bounds (must not exceed 1).
Error in @(x)[P(x,:),XY(x,:)]
Here the desired output would be
C=[1,2,5]

댓글을 달려면 로그인하십시오.

추가 답변 (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