Best way to retrieve items from a cell array using a cell array of indices?

조회 수: 4 (최근 30일)
Hi I am porting a script I had in Python to Matlab. Also returning to MATLAB after a decade, so pardon me if it's too basic.
The key issue I am having is, retrieving premapped items from a cell array. Example, I have:
label_names={"assds", "Sasas", "Asasa", "assds"}
indices={[1 3 2]} %note that this can have varying lengths from 1 to full length of the label array.
label_names(indices) %of course returns an error
Error is "Unable to use a value of type cell as an index."
What is the best way to achieve this - retreive items based on a cell array of indices - as cleanly as possible?
PS: The reason they are cell arrays has to do with varying lengths. To avoid X-Y problem, lets assume rest of the code is done optimally.
Thanks!
  댓글 수: 2
Walter Roberson
Walter Roberson 2025년 1월 16일
If the cell array always has exactly one element, then why use a cell array?
If the cell array potentially has more than one element, then what is the intended meaning? Should the different elements be used as different subscript positions? If the cell array was {[1 3], [2 4]} then should the call be equivalent to X([1 3], [2 4]) producing a 2 x 2 output, or should it be [X(1,2), X(3,4)] producing a 1 x 2 output ?
NV
NV 2025년 1월 17일
Hi Walter,
Thanks. The reason here is, what's shown above happens within a loop. The above two arrays are grabbed from a larger cell array which is why they are of varying lengths. The indices is always a 1D array with list of indices I want to retrieve.
I resolved this after posting this by doing a vertcat as follows:
indices=vertcat(indices{:})
I was hoping there is a direct way to do this (or may be this is the direct way).

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

채택된 답변

Star Strider
Star Strider 2025년 1월 16일
편집: Star Strider 2025년 1월 16일
Creating ‘indices’ as a cell array is the problem.
Refer to it as a double array instead —
label_names={"assds", "Sasas", "Asasa", "assds"}
label_names = 1x4 cell array
{["assds"]} {["Sasas"]} {["Asasa"]} {["assds"]}
indices=[1 3 2] %note that this can have varying lengths from 1 to full length of the label array.
indices = 1×3
1 3 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
label_names(indices)
ans = 1x3 cell array
{["assds"]} {["Asasa"]} {["Sasas"]}
Equivalently (although redundantly) —
label_names={"assds", "Sasas", "Asasa", "assds"}
label_names = 1x4 cell array
{["assds"]} {["Sasas"]} {["Asasa"]} {["assds"]}
indices={[1 3 2]} %note that this can have varying lengths from 1 to full length of the label array.
indices = 1x1 cell array
{[1 3 2]}
label_names(indices{:})
ans = 1x3 cell array
{["assds"]} {["Asasa"]} {["Sasas"]}
.
EDIT — Corrected typographical errors.
.
  댓글 수: 2
NV
NV 2025년 1월 17일
Thanks! This works but it comes in as a cell array due to other reasons, but doing the conversion as follows worked. But I get the point regarding not using cell arrays for this purpose:
indices=vertcat(indices{:})
Star Strider
Star Strider 2025년 1월 17일
As always, my pleasure!
There is cerrtainly nothing wrong with using a cell array if that works for you.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by