find element inside cell
Afficher commentaires plus anciens
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 commentaires
Guillaume
le 1 Mar 2019
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.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!