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

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 Comments

try them in a loop , do u want this all in a single variable
Yes for example ' EECE TOOSTE'
I tried something like
if any(ismember(A(:,1),'MAR'))
A(any(ismember(A(:,1),'MAR'))+1,2)= [A(any(ismember(A(:,1),'MAR')),2) A(any(ismember(A(:,1),'MAR'))+1,2)]
end
but did not work
end
Please, Sabbas, do not write "did not work", but explain what happens. Do you get an error message, if so, which one? Or do the results differ from your expectations?
It would be helpful if you post the input data in valid Matlab syntax, such that we can copy&paste them and test our suggestions. It would be friendly to make it as easy as possible to help you.
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.
ANY(ISMEMBER()) is much less efficient than ANY(STRCMP()).

Sign in to comment.

 Accepted Answer

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.

More Answers (0)

Tags

Asked:

on 9 Jul 2012

Community Treasure Hunt

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

Start Hunting!