I Want to change the last column of a cell 25*5 from characters ie {'Fd,Fc,Fb,Fr'} to numbers, [1,2,3,4]. the code must find for Ex. Fd and replace it with 1 and so on.Thanks

2 次查看(过去 30 天)
I have a cell 1*11 inside which there is 11 cells of dimensions 25*5.I Want to change the last column of a cell 25*5 from characters ie {'Fd,Fc,Fb,Fr'} to numbers, [1,2,3,4]. the code must find for -Ex. Fd and replace it with 1 and so on...
2.27000000000000 240.200000000000 49.6100000000000 0 'Fd'
2.45000000000000 214.610000000000 37.8800000000000 0 'Fd'
4.52000000000000 143.200000000000 6.41000000000000 0 'Fc'
4.60000000000000 51.9000000000000 51.5500000000000 0 'Fc'
4.69000000000000 48.3800000000000 54.9000000000000 0 'Fd'
4.74000000000000 55.0400000000000 24.9100000000000 0 'Fd'
Can someone please help.
Thank you

回答(1 个)

Voss
Voss 2022-4-30
You can do this for each of the 11 cells:
C = { ...
2.27000000000000 240.200000000000 49.6100000000000 0 'Fd'
2.45000000000000 214.610000000000 37.8800000000000 0 'Fd'
4.52000000000000 143.200000000000 6.41000000000000 0 'Fc'
4.60000000000000 51.9000000000000 51.5500000000000 0 'Fc'
4.69000000000000 48.3800000000000 54.9000000000000 0 'Fd'
4.74000000000000 55.0400000000000 24.9100000000000 0 'Fd'};
lookup = {'Fd' 'Fc' 'Fb' 'Fr'};
[ism,idx] = ismember(C(:,end),lookup);
C(ism,end) = num2cell(idx(ism));
disp(C)
{[2.2700]} {[240.2000]} {[49.6100]} {[0]} {[1]} {[2.4500]} {[214.6100]} {[37.8800]} {[0]} {[1]} {[4.5200]} {[143.2000]} {[ 6.4100]} {[0]} {[2]} {[4.6000]} {[ 51.9000]} {[51.5500]} {[0]} {[2]} {[4.6900]} {[ 48.3800]} {[54.9000]} {[0]} {[1]} {[4.7400]} {[ 55.0400]} {[24.9100]} {[0]} {[1]}
M = cell2mat(C) % now (maybe) C can be made into a numeric matrix
M = 6×5
2.2700 240.2000 49.6100 0 1.0000 2.4500 214.6100 37.8800 0 1.0000 4.5200 143.2000 6.4100 0 2.0000 4.6000 51.9000 51.5500 0 2.0000 4.6900 48.3800 54.9000 0 1.0000 4.7400 55.0400 24.9100 0 1.0000
  4 个评论
Voss
Voss 2022-4-30
If you get some values of idx equal to 0 it means some elements of C(:,end) are not in the set of characters lookup, so make sure you are using the right set of characters, because you mentioned these in the question:
{'Fd' 'Fc' 'Fb' 'Fr'}
Then you said it's these:
{'Fd' 'Fc' 'Fo' 'Fr'}
(note the 'Fo' instead of 'Fb')
And make sure they're in the right order, because in your last comment you said "instead of Fr put 3", but 'Fr' is 4th.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by