Search and replace values in cell array?

조회 수: 7 (최근 30일)
Alana Weir
Alana Weir 2016년 8월 16일
댓글: Alana Weir 2016년 8월 16일
I have a cell array that is populated with 0's and letters. I and trying to write a script that will convert the different letters into different numerical values. The array is set up as below;
0 0 0 0 M
0 0 0 0 0
0 S 0 0 0
0 0 L 0 0
And i am trying to convert it to;
0 0 0 0 2
0 0 0 0 0
0 1 0 0 0
0 0 3 0 0
I can't search each column/row and replace manually as I will have an unknown amount of each. I tried using cellfun for this but as the isn't scalar I wasn't sure how. Any suggestions?

채택된 답변

hcai
hcai 2016년 8월 16일
I believe cellfun should still work. Try setting 'UniformOutput' to false.
  댓글 수: 3
hcai
hcai 2016년 8월 16일
Convert it to a matrix first in order to index into your cell array:
A(cell2mat(cellfun(@(elem) elem == 'M', A(:, :), 'UniformOutput', false))) = {2};
Here is what I see:
Alana Weir
Alana Weir 2016년 8월 16일
It worked, thanks!

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by