:cell matrix: transfering the second element of a row to the second element of the next row

2 Ansichten (letzte 30 Tage)
Dear all,
I have the following problem I have a cell matrix A where the first 2 columns are
[1x28 char] [ NaN]
[ NaN] [ NaN]
'MAR' 'EECE'
'PRODEGORY' 'TOOSTE'
[ NaN] [ NaN]
[1x26 char] 'CGATE'
' CONSISIZE' 'COLG 75ML'
' POPUIZE' [1x30 char]
[1x26 char] 'SEDYNE'
' CONSIZE' [1x21 char]
'MAR' 'HYPEKETS'
'PRODTEGORY' 'TOPASTE'
My goal is the following:
IF the first element of a row is ‘MAR’ then transfer the second element of the same row to the second element of the next row.
For instance, rows 3 and 4 should become
'MAR' 'EECE'
'PRODEGORY' ' EECE TOOSTE'
Similarly for the last 2 rows that should be transformed to
'MAR' 'HYPEKETS'
'PRODTEGORY' ' HYPEKETS TOPASTE'
the other rows must remain untransformed
thanks
  5 Kommentare
Sabbas
Sabbas am 9 Jul. 2012
sorry Simon. you are right. What I get is the folowing error
Error using ==> cell.ismember at 28
Input must be cell arrays of strings.
Jan
Jan am 9 Jul. 2012
ANY(ISMEMBER()) is much less efficient than ANY(STRCMP()).

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 9 Jul. 2012
Bearbeitet: Jan am 9 Jul. 2012
C = {'Any string', NaN; ...
NaN, NaN; ...
'MAR', 'EECE'; ...
'PRODEGORY', 'TOOSTE'; ...
NaN, NaN; ...
'Another unknown string', 'CGATE'; ...
' CONSISIZE', 'COLG 75ML'; ...
' POPUIZE', 'no idea what this is'; ...
'Any string with 26 chars', 'SEDYNE'; ...
' CONSIZE', 'A string with 21 chars'; ...
'MAR', 'HYPEKETS'; ...
'PRODTEGORY', 'TOPASTE'};
match = find(strcmp(C(:, 1), 'MAR'));
C(match + 1, 2) = strcat({' '}, C(match, 2), {' '}, C(match + 1, 2));
You did not explain, where the spaces should come from, but I inserted them to match your example ' EECE TOOSTE'.
As you can see, fixing your example data took me more time than creating a solution.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by