Comparing elements of cell arrays

3 ビュー (過去 30 日間)
Jaya
Jaya 2021 年 12 月 2 日
コメント済み: DGM 2021 年 12 月 3 日
My given cell array (p) and existing cell arrays (routes_log) with whom this given one is to be compared are as below.
p={[12 11 13 9]} ; %denotes a route with links 12-11,11-13 &13-9.
routes_log = {[13 9 10]} {[6 11 13 2]} {[12 14 6]} %a 1*3 cell array. Each one denotes a route
In my code, these elements denote graph nodes and so, p cell array denotes a route with links 12-11,11-13 &13-9.
So I want to find if 'ANY' of the given links in array p are a part of 'ANY' of the links of the existing routes. For e.g. in the above case, I want the code to output {[13 9 10]} and {[6 11 13 2]}. As they have the elements 13-9 & 11-13 (consecutively) and my p array also has 13-9 & 11-13. I am confused on how to do this as I cannot use ismember() so straightforwardly.

採用された回答

DGM
DGM 2021 年 12 月 2 日
編集済み: DGM 2021 年 12 月 2 日
Well, I bet there's a more elegant way than this, but this seems to work.
p = {[12 11 13 9]}; % denotes a route with links 12-11,11-13 &13-9.
routes_log = {[13 9 10] [6 11 13 2] [12 14 6]}; % a 1*3 cell array. Each one denotes a route
% build link list as node pairs
lklistp = repelem(p{:},2);
lklistp = reshape(lklistp(2:end-1),2,[]).'
lklistp = 3×2
12 11 11 13 13 9
% compare link lists in logs, build logical mask
shareslinks = false(size(routes_log));
for lg = 1:numel(routes_log)
lklistlog = repelem(routes_log{lg},2);
lklistlog = reshape(lklistlog(2:end-1),2,[]).';
shareslinks(lg) = any(all(ismember(lklistlog,lklistp),2));
end
% apply mask
routeswithcommonlinks = routes_log(shareslinks);
celldisp(routeswithcommonlinks)
routeswithcommonlinks{1} = 13 9 10 routeswithcommonlinks{2} = 6 11 13 2
  4 件のコメント
Jaya
Jaya 2021 年 12 月 3 日
編集済み: Jaya 2021 年 12 月 3 日
Oh yes, find function! Thanks.
I think you meant find(shareslinks). Because find(routeswithcommonlinks) throws an error. Your description is correct but I think by mistake you wrote routeswithcommonlinks instead of shareslinks. But find(shareslinks) works as I want. Thanks a lot!
DGM
DGM 2021 年 12 月 3 日
Oof. Yeah, you got it. See this is what happens when I try to write code in my head.

サインインしてコメントする。

その他の回答 (1 件)

Voss
Voss 2021 年 12 月 2 日
I don't know whether it's feasible to change your code in the way I'm about to suggest, but it may be worthwhile to store the links as n-by-2 matrices with each row denoting the two endpoints of the links, e.g.:
p = {[12 11; 11 13; 13 9]};
routes_log = {[13 9; 9 10] [6 11; 11 13; 13 2] [12 14; 14 6]};
Then you could use ismember with the 'rows' option to check for existing links.
  2 件のコメント
dpb
dpb 2021 年 12 月 2 日
Agree w/ @Benjamin. The storage scheme you have is memory-efficient, but exceedingly difficult to use for the stated purpose. Redesign the storage around the need is also my recommendation.
Otherwise you're into nested cellfun calls with sequential sections of the cell to be compared -- ugly at best.
Jaya
Jaya 2021 年 12 月 3 日
I get it. If I understand correctly, the answer by @DGM seems to do what you are suggesting. As I would use his code to integrate in my own code and as I can accept only 1 answer I am accepting his. But thanks for your suggestions.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by