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 CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by