필터 지우기
필터 지우기

Indexing structure without using scalars

조회 수: 1 (최근 30일)
Milos
Milos 2012년 10월 31일
Consider following structure:
mm(1,1).no=1;
mm(2,1).no=2;
mm(3,1).no=3;
mm(3,1).mtx=[3;3;3];
mm(2,1).mtx=[2;2;2];
mm(1,1).mtx=[1;1;1];
a1 = cat(1, mm([1,2]).no)
a1 =
1
2
a2 = cat(1, mm([1,2]).mtx(1))
Scalar index required for this type of multi-level indexing.
Is there a work-around for this type of indexing?
  댓글 수: 2
Matt J
Matt J 2012년 10월 31일
If your mtx and no data re all the same size, as in this example, it makes more sense to hold them in a scalar struct
mm.no=[1;2;3];
mm.mtx=[1 2 3; 1 2 3; 1 2 3];
Then you can easily extract pieces that you need
a1=mm.no(1:2);
a2=mm.mtx(:,1);
Milos
Milos 2012년 11월 1일
This was just an example structure. Data is different, and matrices are way larger.

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

채택된 답변

Sean Little
Sean Little 2012년 10월 31일
Unfortunately, Matlab does not support multi-level indexing like other languages do. You are probably going to have to create an intermediate variable.
a1 = [mm(1:2).mtx];
a2 = a1(1,:)
  댓글 수: 1
Milos
Milos 2012년 11월 1일
Thanks, that's what I feared.

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

추가 답변 (1개)

Matt J
Matt J 2012년 10월 31일
tmp=cat(1,mm([1,2]).mtx);
a2=tmp(1)

카테고리

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