Replace numbers by strings
39 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
wave_buoys
am 14 Jan. 2018
Kommentiert: Jos (10584)
am 15 Jan. 2018
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 Kommentare
Stephen23
am 14 Jan. 2018
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
Akzeptierte Antwort
per isakson
am 14 Jan. 2018
Bearbeitet: per isakson
am 14 Jan. 2018
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 Kommentar
Jos (10584)
am 15 Jan. 2018
Great and simple answer by Per. A minor suggestion:
A = [80 90 100] % data
C = repmat({'case1'}, size(A))
C(A>=90) = {'case2'}
Weitere Antworten (1)
Jan
am 14 Jan. 2018
Or:
Pool = {'case1', 'case2'};
B = Pool((A < 90) + 1)
Or:
B = sprintfc('case%d', (A < 90) + 1)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!