I have a cell array A and a cell array B and want to re-order info in A according to B.

1 vue (au cours des 30 derniers jours)
I have a cell array A {1:2,1:23} and a cell array B {1,1:300}. B is the reference standard to re-order A. The cell array A {1,1:23} contains a small subset of uid information of B {1,1:300}, but in a random order. I also have geometrical information in A {2, 1:23} corresponding to A {2, 1:23} but, not included in B at all. What I want to do is to create a cell array or matrix C, with all information of A {1,1:23} contained in B, re-ordered. The re-ordering needs to follow the sequence of appearance of elements A in B (hence, the exact order the elements A appear in B sequentially re-ordered). I then need to re-order A {2,1:23} in the same way (via indexing?). Any ideas would be very much appreciated! I attach A and B.

Réponse acceptée

Chris Perkins
Chris Perkins le 3 Mai 2018
Here's one approach you could use to re-order the elements of A in a new cell, C:
% Assuming A and B are already loaded into your Workspace
C = cell(size(B));
for i = 1:size(A, 2) % for each element in A
C(find(strcmp(B, A{1,i}))) = {A{2,i}}; % Put the element from A in the right place in C
end
C(cellfun('isempty',C)) = []; % Remove empty elements
Here, we make C the same size as B, put every element from A in the correct place in C, and then remove the empty elements, so C ends up holding only the matches from A to B.
Note: I ran this with your data, and it appears that some uid's in A are not present in B (only 16 of the 23 elements in A were matched to indexes in B). So, in this case, C becomes a 1x16 cell.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by