find element inside cell

조회 수: 14 (최근 30일)
NA
NA 2019년 3월 1일
댓글: Guillaume 2019년 3월 1일
A={[1,2,4],[2,3,4],[2,4,5],[4,5,6,9,10,11],[4,7,9],[7,8],[6,12,13],[4,5,6,7]};
base={[2],[1],[1],[3],[2],[1],[1],[1]};
first_element=cellfun(@(v)v(1),A,'UniformOutput',false);
result=cellfun(@(m,v)m(find(v==1)),A,base,'UniformOutput',false);
I want to change order of cell A, according to base. base that has [1] should come first
result should be
A={[2,3,4],[2,4,5],[7,8],[6,12,13],[4,5,6,7],[1,2,4], [4,7,9],[4,5,6,9,10,11]};
  댓글 수: 2
Guillaume
Guillaume 2019년 3월 1일
result should be A={[2,3,4],[2,4,5],[7,8],[6,12,13],[4,5,6,7],[1,2,4], [4,7,9],[4,5,6,9,10,11]};
Your code is never going to produce anything like that, and it's really not clear how you'd get that result with the base you've given. You could obtain that result with:
result = A([2, 3, 6, 7, 8, 1, 5, 4])
As can be clearly seen in the text of the error message, the code that produces the error is not the same code you show in your question.
%code in the question
result = ...
%code that produces the error
[~, ids] = ...
And yes, [~, ids] = ... is going to produce an error as you ask for the second output of something that only produces one.
All in all, it's very unclear what you're trying to do. It would be simpler if you told gave us details of the general problem.
NA
NA 2019년 3월 1일
편집: NA 2019년 3월 1일
base of [1,2,4] is 2(index 2). base of [2,4,5], is 2 (index 1). for [4,5,6,9,10,11] base is index 3, (6)
A={[1,2,4],[2,3,4],[2,4,5],[4,5,6,9,10,11],[4,7,9],[7,8],[6,12,13],[4,5,6,7]};
index_base={[2],[1],[1],[3],[2],[1],[1],[1]};
I want to arrange A according to the index_base that has 1.
[1,2,4] has index 2 as base so, I have to move it to the end of cell A.
[2,3,4] has index 1 as base so, it should come first in result.
[2,4,5] has index 1 as base so, it should come after [2,3,4] .
[4,5,6,9,10,11] has index 3 as base so, I have to move it to the end of cell A after [1,2,4] .
[4,7,9] has index 2 as base so, I have to move it to the end of cell A .
[7,8] has index 1 as base so, it should come after [2,4,5]

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

채택된 답변

Guillaume
Guillaume 2019년 3월 1일
Oh! That's easy then.
First, there's no need for index_base to be a cell array. It uses 15 times more memory than a simple vector for no benefit.
index_base = [2 1 1 3 2 1 1 1]; %use exactly 15 times less memory than a cell array
If for some reason, it has to start as a cell array, we do need it as a matrix anyway, so:
%index_base = {2 1 1 3 2 1 1 1};
index_base = cell2mat(index_base); %we need a matrix
Then,
[~, order] = sort(index_base);
result = A(order);
  댓글 수: 1
Guillaume
Guillaume 2019년 3월 1일
I think you would be better off starting a new question for that.
If the code you've written doesn't do what you want I would recommend that you don't put it in the question as that's a bit confusing. Rather give the starting point (A, ref, and index_base if it's needed), what you want to obtain and explain in words how to get there (bullet points detailing the steps would be ideal).

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

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