gpt4 book ai didi

matlab - 如何在复杂的单元格中查找元素?

转载 作者:太空宇宙 更新时间:2023-11-03 20:06:27 25 4
gpt4 key购买 nike

我有一个复杂的元胞数组,例如:

A = {1 {2; 3};4 {5 6 7;8 9 10}};

如何找到 A 中的元素?例如,我想检查 9 是否在 A 中!!

最佳答案

如果您的元胞数组可以有任意数量的嵌套级别,您将不得不递归所有这些级别以检查值。这是一个可以执行此操作的函数:

function isPresent = is_in_cell(cellArray, value)

f = @(c) ismember(value, c);
cellIndex = cellfun(@iscell, cellArray);
isPresent = any(cellfun(f, cellArray(~cellIndex)));

while ~isPresent
cellArray = [cellArray{cellIndex}];
cellIndex = cellfun(@iscell, cellArray);
isPresent = any(cellfun(f, cellArray(~cellIndex)));
if ~any(cellIndex)
break
end
end

end

此函数将检查非元胞数组的条目的值,然后提取元胞数组的条目以移除一个嵌套层。重复此操作,直到不再有元胞数组条目或找到该值。

关于matlab - 如何在复杂的单元格中查找元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30038476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com