簡體   English   中英

修改大單元格數組以查找滿足MATLAB條件的某些行

[英]Modify large cell array to find certain rows that meet a condition in MATLAB

我有一個單元矩陣,我想在MATLAB中分析某些行。 我自己的解決方案非常糟糕(見底部),所以我認為有人可以給我一個如何改進它的提示。 我很確定,我只需要以某種方式重塑單元陣列,但我不太清楚如何做到這一點。

我有一個帶有邏輯數組條目的單元格:

TheCell{with N-rows cell}(Logical Array with varying length, but max 24 entries)

例如:

TheCell{1} = [0,1,0,1,0,0,0,0,0,1,0]
TheCell{2} = [0,0,0,0]
...
TheCell{9} = [0,1,0,0,0,0,0]

此外,我有一個名為Problem的矩陣,它告訴我“TheCell”中哪些行我感興趣(問題矩陣存儲了一些特定的TheCell行索引):

Problem(with M-rows)

例如:

Problem = [3,5,9]

我想找到TheCell的所有單元格條目(索引),其中“1”出現在以下任一位置,:

Critical = [1;2;3;4;5;6]

因此,例如在行問題(3)中,即TheCell {9},條件在Critical(2)處得到滿足,因為:

TheCell{Problem(3)}(Critical(2)) == 1

因此,我可以在我的解決方案矩陣中創建一個新條目:

Solution(counter) = Problem(3)

最后,我在一個糟糕的解決方案中實現了這個,而不是真正有效。

Critical = [1;2;3;4;5;6];
Solution = [];
counter = 1;
for i = 1:length(Problem)
   Row = Problem(i);
   b = length(TheCell{Row})
   for k = 1:length(Critical)
        if k > b
           break;
        end  
        if TheCell{Row}(Critical(k))==1
            Solution(counter) = Row;
            counter = counter+1;
            break;
        end
    end
end
Critical = 6;
find(cellfun(@(x)any(x(1:min(Critical,end))), TheCell))

或者,如果Critical不會始終是從1開始的連續數字

Critical = [2,4,5];
find(cellfun(@(x)any(x(min(Critical,end))), TheCell))

如果你可以控制如何創建TheCell ,你可以通過不使用單元格數組來獲得更有效的解決方案,而是用false填充每行的末尾。

例如

TheMatrix = false(9,24);

%// You would generate this more sensibly in your process
TheMatrix(1,1:11) = [0,1,0,1,0,0,0,0,0,1,0]
TheMatrix(2,1:4) = [0,0,0,0]
...
TheMatrix(9,1:7) = [0,1,0,0,0,0,0]

然后解決方案是:

find(any(TheMatrix(:,Critical),2))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM