find array in cell using cellfun
5 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 23 Jan 2019
See also earlier related thread:
Accepted Answer
Andrei Bobrov
on 23 Jan 2019
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 Comments
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!