필터 지우기
필터 지우기

How to replace the contents of a cell array

조회 수: 2 (최근 30일)
Dubstep Dublin
Dubstep Dublin 2016년 1월 26일
댓글: Triveni 2016년 1월 28일
I have a cell array of 2 columns as follows
Column1 Column2
AA BB0p0
AA BB0p5
AA BB1p0
AA BB1p5
...and so on
I would like to replace the contents of the 2nd column as follows
BB0p0 = 0
BB0p5 = 1
BB1p0 = 2
BB1p5 = 3 and so on...
I am a matlab newbie, so my apologies if any part of the question is not clear.
Thanks
  댓글 수: 1
Dubstep Dublin
Dubstep Dublin 2016년 1월 26일
I think it all boils down to using strrep on a cell array. Can someone please tell me how to do that?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2016년 1월 26일
[unique_col2, ~, col2_idx] = unique(YourCell(:,2));
col2_idx_cell = num2cell(col2_idx - 1); %the index is 1 based rather than 0 based
YourCell(:,2) = col2_idx_cell;
And the map back of any one stored value is unique_col2{stored_value+1}
Typically you would skip the -1 and +1 in the above and use references starting from 1, but using references starting from 0 is not wrong, and has its uses.
  댓글 수: 5
Dubstep Dublin
Dubstep Dublin 2016년 1월 27일
Yes it works! Thanks a lot Walter.
I have a followup question: What do I do if I have to replace the contents of the cell as follows.
BB0p0 = 0 BB0p5 = 0.5 BB1p0 = 1.0 BB1p5 = 1.5 and so on...
Triveni
Triveni 2016년 1월 28일
At-least you should accept answers when someone help....accept walter's answer

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by