Search code examples
matlabvectorcomparisongrid-layoutcell-array

Using ismember() with cell arrays containing vectors


I am using a cell array to contain 1x2 vectors of grid locations in the form [row, col]. I would like to check if another grid location is included in this cell array.

Unfortunately, my current code results in an error, and I cannot quite understand why:

in_range = ismember( 1, ismember({[player.row, player.col]}, proximity(:,1)) );

where player.row and player.col are integers, and proximity's first column is the aforementioned cell array of grid locations

the error I am receiving is:

??? Error using ==> cell.ismember at 28
Input must be cell arrays of strings.

Unfortunately, I have not been able to find any information regarding using ismember() in this fashion, only with cell arrays as strings or with single integers in each cell rather than vectors.

I have considered converting using num2str() and str2num(), but since I must perform calculations between the conversions, and due to the number of iterations the code will be looped for (10,000 loops, 4 conversions per loop), this method seems prohibitive.

Any help here would be greatly appreciated, thank you

EDIT: Why does ismember() return this error? Does it treat all vectors in a cell array as string arrays?

EDIT: Would there be a better / more efficient method of determining if a 1 is in the returned vector than

ismember( 1, ismember(...))?


Solution

  • I'm short of time at the moment (being Chrissy eve and all), so this is going to have to be a very quick answer.

    As I understand it, the problem is to find if an x y coordinate lies in a sequence of many x y coordinates, and if so, the index of where it lies. If this is the case, and if you're interested in efficiency, then it is wasteful to mess around with strings or cell arrays. You should be using numeric matrices/vectors for this.

    So, my suggestion: Convert the first row of your cell array to a numeric matrix. Then, compare your x y coordinates to the rows of this numerical matrix. Because you only want to know when both coordinates match a row of the numerical matrix, use the 'rows' option of ismember - it will return a true only on matching an entire row rather than matching a single element.

    Some example code that will hopefully help follows:

    %# Build an example cell array with coordinates in the first column, and random strings in the second column
    CellOfLoc = {[1 2], 'hello'; [3 4], 'world'; [5 6], '!'};
    
    %# Convert the first column of the cell array to a numerical matrix
    MatOfLoc = cell2mat(CellOfLoc(:, 1));
    
    %# Build an example x y coordinate location to test
    LocToTest = [5 6];
    
    %# Call ismember, being sure to use the rows option
    Index = ismember(MatOfLoc, LocToTest, 'rows');
    

    Note, if the indices in your cell array are in string form, then obviously you'll also need a call to str2num in there somewhere before you call ismember.

    One other thing, I notice you're a new member, so welcome to the site. If you think this response satisfactorily answered your question, then please mark the question answered by clicking the tick mark next to this response.