How do I index an array/cell array with an array?

조회 수: 1 (최근 30일)
Lockywolf
Lockywolf 2015년 11월 29일
답변: Image Analyst 2015년 11월 29일
I am sorry, it's a very newbie question, but I am nearly lost.
So I have a cell array
data2 = num2cell(magic(5))
And I have an array of size 2.
coord = [x, y]
I want to obtain data2{x,y}
without writing data2{coord(1), coord(2)}
Is it possible to do that?
data2{ coord } gives a completely different result.

답변 (2개)

Star Strider
Star Strider 2015년 11월 29일
You have to write ‘data2{coord(1), coord(2)}’. That’s just the way indexing works.

Image Analyst
Image Analyst 2015년 11월 29일
If coord is really a 2 element array and not some larger matrix, then you could use sub2ind() but honestly I think that would just make it more cryptic and less maintainable than doing data2{coord(1), coord(2)}.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by