find array in cell using cellfun

5 次查看(过去 30 天)
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

请先登录,再进行评论。

采纳的回答

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 CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by