How to extract column information of a mix cell array! - Please Help :(
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Wilson Weng
el 2 de Sept. de 2017
Comentada: Wilson Weng
el 14 de Sept. de 2017
Hi the wonderful MatLab community!
Im very new to MatLab and urgently require some assistance please!
I have a cell array
C=
1 '100'
2 '200'
3 '200'
4 '100'
5 '200'
...etc
The cell array changes will always have the two columns but will have random number of rows. The first column will always start from 1 and increase in sequential order moving down the cell array. The second column will only have numbers of either '100' or '200'.
How do you convert array of C such that C(:,2) == 200; basically outcome of array D to be:
D=
2
3
5
...etc
0 comentarios
Respuesta aceptada
Henry Giddens
el 2 de Sept. de 2017
Hi,
In this example, all of your '100' and 200' values are strings, so you need to identify the cells in the second column of your cell array which have the string value of 200.
The following line of code does this, and also groups the output into an array of numerical values (rather than a cell array).
D = [C{strcmp(C(:,2),'200'),1}]'
3 comentarios
Henry Giddens
el 5 de Sept. de 2017
Editada: Henry Giddens
el 5 de Sept. de 2017
If I have understood this correctly:
The above answer should give you all the entries in the cell array with only '200' in the middle column edited slightly because you are also interested in the final column now:
indx = C{strcmp(C(:,2),'200');
C2 = C(indx,:);
Now loop through the third column, and see if the same number appears in the firstcolumn:
indx2 = false(length(C2),1);
for i = 1:length(C2);
if ismember(C2{i,3},[C2{:,1}]);
indx2(i) = true;
end;
end
% select the rows from C2 that have been identified as meeting the criteria
% and convert to numerical array:
D = cell2mat([C2(indx2,1),C2(indx2,3)])
D =
4 1
7 4
Más respuestas (1)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!