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
1 次查看(过去 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
0 个评论
回答(1 个)
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)
M = cell2mat(C) % now (maybe) C can be made into a numeric matrix
4 个评论
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 Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!