find element inside cell

1 回表示 (過去 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 Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by