find array in cell using cellfun

4 ビュー (過去 30 日間)
NA
NA 2019 年 1 月 23 日
回答済み: Andrei Bobrov 2019 年 1 月 23 日
I have
A = {[1,2,4],[2,3,4],[1,2,5]};
mes_in=[12,18,15,14,13]
B=[1 2;...
2 1;...
1 4;...
4 1;...
1 5;...
5 1;...
2 3;...
3 2;...
2 4;...
4 2;...
2 5;...
5 2;...
3 4;...
4 3]
mes_between=[12;12.2;13;13.8;9;9.1;4;4.2;5;5.8;6;6.8;7;7.8]
I want calulate:
for
A=[1,2,4]
mes_in(1)=12=A{1}(1) , mes_in(2)=18=A{1}(2), mes_in(4)=14=A{1}(4)
start from first element in A: 1
mes_in(1)=12,
F = @(v)find(any(v==B(:,1),2)&~any(v==B(:,2),2));
C = cellfun(F,A,'uni',0) % I can calculate index 5
mes_between(5)=9
mes_true=mes_in(1)-mes_between(5)=12-9=3
start from second element in A: 2
mes_true=mes_in(2)-mes_between(7)-mes_between(11)=18-4-6=8
start from second element in A: 4
mes_true=mes_in(4)-mes_between(14)=14-7.8=6.2
I use this one but it is not working
F = @(v)find(any(v==B(:,1),2)&~any(v==B(:,2),2));
C = cellfun(F,A,'uni',0)
mes_true=cellfun(@(n,i) c-mes_between(i),mes_in,C,'uni' ,0);
  2 件のコメント
NA
NA 2019 年 1 月 23 日
編集済み: NA 2019 年 1 月 23 日
F = @(v)find(any(v==B(:,1),2)&~any(v==B(:,2),2));
C = cellfun(F,A,'uni',0)
gives C=[5;7;11;14] for A{1}.
I want to seperate [5;7;11;14], as such
mes_true=mes_in(1)-mes_between(5)=12-9=3
mes_true=mes_in(2)-mes_between(7)-mes_between(11)=18-4-6=8
mes_true=mes_in(4)-mes_between(14)=14-7.8=6.2
Stephen23
Stephen23 2019 年 1 月 23 日

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2019 年 1 月 23 日
A = {[1,2,4],[2,3,4],[1,2,5]};
mes_in=[12,18,15,14,13];
B=[1 2;...
2 1;...
1 4;...
4 1;...
1 5;...
5 1;...
2 3;...
3 2;...
2 4;...
4 2;...
2 5;...
5 2;...
3 4;...
4 3];
mes_between=[12;12.2;13;13.8;9;9.1;4;4.2;5;5.8;6;6.8;7;7.8];
out = cellfun(@(x)fun(x,B,mes_in(:),mes_between(:),(1:size(B,1))'),A,'un',0);
function out = fun(x,B,mes_in,mes_between,k)
b = x(:)' == permute(B,[1,3,2]);
lo = any(b,2);
p = k.*b(:,:,1);
[~,jj,v] = find(p(lo(:,:,1)&~lo(:,:,2),:));
out = mes_in(x) - accumarray(jj,mes_between(v),[numel(x),1]);
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by