find array in cell using cellfun

4 vues (au cours des 30 derniers jours)
NA
NA le 23 Jan 2019
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 commentaires
NA
NA le 23 Jan 2019
Modifié(e) : NA le 23 Jan 2019
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 le 23 Jan 2019

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 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

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by