0

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

1
  • do these values '1', '2', '3' not exist in your original array? If those values are coming from my_des, why dont you simply select the second column? Commented Dec 7, 2017 at 9:30

1 Answer 1

1

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'};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.