简体   繁体   中英

Indexing a cell in MATLAB with a vector

Assume that I am given the cell M , and a vector v , and assume that v holds a correct address for an element in M . Without any apriori knowledge of the structure of M and v , I wish to fetch the element from M that v points to. How can I do this? For instance, M{v} does not quite do what I need:

say M = cell(3,4,5) , v = [1,2,3] , then doing M{v} produces [M{1}, M{2}, M{3}] .

So my question is: how can I encapsulate an address of a cell element into a variable v and later fetch the element at that address from the given cell?

The same question for any array instead of cell.

v has to be a cell not a vector, then you can use sub2ind :

v=mat2cell(v(:),ones(numel(v),1))
M(sub2ind(size(M),v{:}))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM