Two cell arrays indexing

조회 수: 1 (최근 30일)
Ko Fa
Ko Fa 2020년 11월 10일
댓글: Ko Fa 2020년 11월 11일
I have two cell Arays, lets say
A = {[],[],[2,3],[1,2]}
B = {[1,2,3],[4,5],[6,7,8],[9,10,11]}
"A" contains the Index of the numbers I am trying to get out of "B". So the [2,3] from A corresponds to the numbers [7,8] from B.
Now I would like my output to be:
output = {[],[],[7,8],[9,10]}
My original data is much larger than this, so ideally a general solution would be great.
Any help is greatly appreciated.

채택된 답변

dpb
dpb 2020년 11월 10일
편집: dpb 2020년 11월 10일
C=cellfun(@(a,b)b(a),A,B,'UniformOutput',false);
It's just logical indexing in a background loop under the guise of cell addressing. Seems more complicated than actually is.
>> C=cellfun(@(a,b)b(a),A,B,'UniformOutput',false);
>> C{:}
ans =
[]
ans =
[]
ans =
7 8
ans =
9 10
>>
  댓글 수: 1
Ko Fa
Ko Fa 2020년 11월 11일
Thank you! I did come up with this solution myself after some time:
V = cell(1,length(A));
k = 1;
for i = 1:length(A)
V{k} = B{i}([A{i}]);
k = k+1;
end
C = V
Certainly your solution is more advanced and I will be using it.

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

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