Filter löschen
Filter löschen

Retrieving indexing of a cell

2 Ansichten (letzte 30 Tage)
Eli Dim
Eli Dim am 29 Jun. 2015
Kommentiert: Eli Dim am 29 Jun. 2015
I have the variable NameMatrix < nx5 cell > where the number of columns(5) always stays the same and the number of rows(n) changes.
For n=4, NameMatrix looks like this:
'A1' 'A2' 'A3' 'A4' 'A5'
'B1' 'B2' 'B3' 'B4' 'B5'
'C1' 'C2' 'C3' 'C4' 'C5'
'D1' 'D2' 'D3' 'D4' 'D5'
For n=11, NameMatrix looks like this:
'A1' 'A2' 'A3' 'A4' 'A5'
'B1' 'B2' 'B3' 'B4' 'B5'
'C1' 'C2' 'C3' 'C4' 'C5'
'D1' 'D2' 'D3' 'D4' 'D5'
'E1' 'E2' 'E3' 'E4' 'E5'
'F1' 'F2' 'F3' 'F4' 'F5'
'G1' 'G2' 'G3' 'G4' 'G5'
'H1' 'H2' 'H3' 'H4' 'H5'
'I1' 'I2' 'I3' 'I4' 'I5'
'J1' 'J2' 'J3' 'J4' 'J5'
'K1' 'K2' 'K3' 'K4' 'K5'
I do some operations with NameMatrix and as a result for the case where n=11, the new matrix looks like this
'F1'
'F2'
'F3'
'F4'
'F5'
'G1'
'G2'
'G3'
'G4'
'G5'
'H1'
'H2'
'H3'
'H4'
'H5'
'I1'
'I2'
'I3'
'I4'
'I5'
'J1'
'J2'
'J3'
'J4'
'J5'
'K1'
'K2'
'K3'
'K4'
'K5'
'A1'
'A2'
'A3'
'A4'
'A5'
'B1'
'B2'
'B3'
'B4'
'B5'
'C1'
'C2'
'C3'
'C4'
'C5'
'D1'
'D2'
'D3'
'D4'
'D5'
'E1'
'E2'
'E3'
'E4'
'E5'
I would like to extract the indices of this new matrix. So F1 = 26, F2 =27, F3=28, F4=29, F5=30, G1=31, G2=32, G3=33, etc. SO i get a new vector which looks like this [26 27 28 29 30 31 32 33....] for an arbitrary number of rows n. Which would be the best way to do this?

Akzeptierte Antwort

Guillaume
Guillaume am 29 Jun. 2015
Use the second return value of ismember.
Note that linear indices in matlab are per column rather than per row (so the indices of 'F1', 'F2', F3', etc. would be [6, 17, 28, ...]. If you do want [26, 27, ...] you'll have to transpose your initial matrix:
c = {'A1' 'A2' 'A3' 'A4' 'A5'
'B1' 'B2' 'B3' 'B4' 'B5'
'C1' 'C2' 'C3' 'C4' 'C5'
'D1' 'D2' 'D3' 'D4' 'D5'
'E1' 'E2' 'E3' 'E4' 'E5'
'F1' 'F2' 'F3' 'F4' 'F5'
'G1' 'G2' 'G3' 'G4' 'G5'
'H1' 'H2' 'H3' 'H4' 'H5'
'I1' 'I2' 'I3' 'I4' 'I5'
'J1' 'J2' 'J3' 'J4' 'J5'
'K1' 'K2' 'K3' 'K4' 'K5'};
cc = {'F1' 'F2' 'F3' 'F4' 'F5' 'G1' 'G2' 'G3' 'G4' 'G5' 'H1' 'H2' 'H3' 'H4'}';
[~, indices] = ismember(cc, c) %transpose c to get [26 27 ...]
  2 Kommentare
Eli Dim
Eli Dim am 29 Jun. 2015
Thank you for your answer. I followed your instructions, but I keep on getting the following error:
Error using cell/ismember>cellismemberR2012a (line 199) Input A of class cell and input B of class cell must be cell arrays of strings, unless one is a string.
Error in cell/ismember (line 71) [varargout{1:nlhs}] = cellismemberR2012a(varargin{:});
Do you have any idea what is wrong?
Eli Dim
Eli Dim am 29 Jun. 2015
Never mind, I figured it out. Thank you very much for your quick answer.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by