Reputation: 6668
I have a cell array which contains some descriptions, namely my_des
.
my_des = [{'FRD'} {'1'}; {'UNFRD'} {'2'}; {'OTH'} {'3'};];
I also have an approximately 5000x1 cell array. The elements in this array are either 'FRD'
, 'UNFRD'
or 'OTH'
.
What I want to do is replace these text values with the corresponding numeric values in my_des
.
Currently my only idea (which I think isn't that great) is to loop through my_des
and do a string replacement.
Example:
So say my current vector looks like this:
FRD
FRD
OTH
UNFRD
OTH
FRD
Then my desired output would be this:
1
1
3
2
3
1
The numbers come from the my_des
array
Upvotes: 0
Views: 458
Reputation: 30165
Do you want to use the characters '1'
, '2'
, '3'
or just the numbers 1
, 2
, 3
? The distinction is the difference between a 1 line answer and a 2 line answer!
Based on your example, let's use the following data:
arr = {'FRD'; 'FRD'; 'OTH'; 'UNFRD'; 'OTH'; 'FRD'};
Get the row index within my_des
of each element in arr
, and use that to get the corresponding 2nd column values...
% If you just want the *number* then this is all you need
[~, idx] = ismember(arr, my_des);
% idx is the row within column 1 of my_des where the value in arr is found
% >> idx = [1; 1; 3; 2; 3; 1]
% If you want to get the values my_des then use idx as a row index
out = mydes(idx, 2);
% out is the corresponding values from the 2nd column of my_des, whatever they may be.
% >> out = {'1'; '1'; '3'; '2'; '3'; '1'};
Aside: why are you declaring a cell array by concatenating 1-element cell arrays for my_des
? Instead, you can just do this:
my_des = {'FRD', '1';
'UNFRD', '2';
'OTH', '3'};
Upvotes: 1