1

I have a cell array {'W','L','D','D','W'}. I want to convert this into a {-1,0,1} array where 'W' is mapped to 1, 'D' to 0, and 'L' to -1.

Is there a quick way to do this without writing a loop?

3 Answers 3

6

You could use categorical arrays to do this in a single expression

double(categorical({'W','L','D','D','W'}, {'L', 'D', 'W'})) - 2

Or for MATLAB prior to R2013b, you can do two expressions:

[~, loc] = ismember({'W','L','D','D','W'}, {'L', 'D', 'W'});
result = loc - 2;
Sign up to request clarification or add additional context in comments.

3 Comments

Clever! It should be noted that categorical arrays were introduced in R2013b.
Sorry, introduced to base MATLAB in R2013b, I believe they have been part of the stats toolbox for a while.
Yep, added the alternative for MATLAB-only prior to R2013b.
4

use strcmp :

A = {'W','L','D','D','W'};
B = strcmp (A,'W');
C = strcmp (A,'L') * -1;
B+C

ans =

 1    -1     0     0     1

1 Comment

You could also index directly with the result of strcmp: B(strcmp(A, 'W')) = 1;
0

@GameOfThrows's answer is good but it would not generalize well should your character-number mapping become more complex. Here is my solution:

Create a function that maps the characters the way you want

function n = mapChar(c)
    if strcmp(c, 'W')
        n = 1;
    elseif strcmp(c, 'D')
        n = 0;
    elseif strcmp(c, 'L')
        n = -1;
    end
end

and then use cellfun to apply it over the cell array:

>> cellfun(@mapChar, {'W','L','D','D','W'})

ans =

     1    -1     0     0     1

If you find that you need to use a different mapping, you just redefine your mapping.

Also, it all can be inlined (using part of the @GameOfThrows's approach):

>> cellfun(@(c)(strcmp(c, 'W') - strcmp(c, 'L')), {'W','L','D','D','W'})

ans =

     1    -1     0     0     1

4 Comments

None of that is necessary, strcmp works over cell arrays already and you can use the logical return to index your result directly. e.g. B(strcmp(A, 'W')) = 1;
@excaza I know, but, as I have written at the beginning of my answer, the motivation for using a separate function is that the mapping might become more complex than just strcmps. In that case it would be much easier to rewrite the one function rather than inventing a new way of how to do it with the whole arrays (which may become non-trivial). The inline version is just a bonus and, of course, is better performed with the whole arrays.
This gist is functionally equivalent and does not require logic control nor cellfun.
A switch statement would be more natural than if/else indoor first example.

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.