简体   繁体   中英

How to find the row index of a cell array that matches certain row of cells in MATLAB?

Say I have a cell array

CELLARRAY = 
{{[]} {[]} {[1 1 1]} {[]} {[]} {[1 1 1]};

{[]} {[]} {[1 1 1]} {[]} {[]} {[]};

{[]} {[]} {[]} {[]} {[]} {[]}}


A = {{[]} {[]} {[1 1 1]} {[]} {[]} {[]}}

Is there a smart way of finding the row index within CELLARRAY that matches A ? and my answer would be 2?

Probably the fastest way is going to be a for loop through the rows and MATLAB's JIT compiler should be able to optimize that decently.

matches = false(1, size(CELLARRAY, 1));

for k = 1:size(CELLARRAY, 1)
    matches(k) = isequal(CELLARRAY(k,:), A);
end

indices = find(matches);

Alternately, you could use something like cellfun to perform the operation for you but it will likely be slower

matches = cellfun(@(x)isequal(x, A), num2cell(CELLARRAY, 2)); 
indices = find(matches)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM