필터 지우기
필터 지우기

Replace numbers by strings

조회 수: 27 (최근 30일)
wave_buoys
wave_buoys 2018년 1월 14일
댓글: Jos (10584) 2018년 1월 15일
Hi,
I have a matrix A= 1x100, and now I want to replace if numbers in A < 90 by 'case1' and A>=90 by 'case 2'
Could anyone offer some help?
Thanks
  댓글 수: 2
per isakson
per isakson 2018년 1월 14일
Strings, e.g. 'case1', cannot be stored in a numerical matrix.
Stephen23
Stephen23 2018년 1월 14일
htran's "Answer" moved here:
I mean probably I have to create a matrix B 1x100 which contains strings: 'case1' and 'case 2'. For example:
A B
10 case 1
100 case 2
40 case 1
.. so on
But I don't know how to a new matrix B according to numbers in matrix B.
More help please

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

채택된 답변

per isakson
per isakson 2018년 1월 14일
편집: per isakson 2018년 1월 14일
One way:
A = randi([80,100], 1,100 ); % Sample data
C = cell(size(A));
C(A<90)={'case1'};
C(A>=90)={'case2'};
result
>> C(1:5)
ans =
'case1' 'case2' 'case2' 'case2' 'case1'
  댓글 수: 1
Jos (10584)
Jos (10584) 2018년 1월 15일
Great and simple answer by Per. A minor suggestion:
A = [80 90 100] % data
C = repmat({'case1'}, size(A))
C(A>=90) = {'case2'}

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

추가 답변 (1개)

Jan
Jan 2018년 1월 14일
Or:
Pool = {'case1', 'case2'};
B = Pool((A < 90) + 1)
Or:
B = sprintfc('case%d', (A < 90) + 1)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by