필터 지우기
필터 지우기

Find all combinaisons of sum of two vector that corresponds to the value of a third vector

조회 수: 1 (최근 30일)
Hi community !
I have three vectors A, B and C.
A = [1561.28, 1561.64, 1562.00, 1562.36, 1562.72]; % signal
B = [1064.00, 1064.36, 1064.72, 1065.08, 1065.44]; % pump
C = [632.89, 633.02, 633.14, 633.27, 633.40]; % converted
I want to check all the possible combinaisons of A and B (see next function) that correspond to a value of C
func = 1./(1./A + 1./B) >> C
For example a correct anwser in the result vector would be [3 1 1] for :
1/( 1/A(3) + 1/B(1) ) = C(1)
I've already check some functions like nchoosecrit() but it only take one scalar as an result argument so I don't know.

채택된 답변

Matt J
Matt J 2019년 6월 18일
편집: Matt J 2019년 6월 18일
tolerance=1e-6;
[A,B,C]=deal(A(:),B(:),C(:)); %Make everything a column vector
[Ar,Br,Cr]=deal(A,B.' , reshape(C,1,1,[]) );
lidx = abs( 1./Ar + 1./Br.' - 1./Cr ) <= tolerance ;
[i,j,k]=ind2sub(size(lidx), find(lidx));
locations=[i,j,k],
combinations=[A(i),B(j),C(k)]
  댓글 수: 3

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Adam
Adam 2019년 6월 18일
Well, what you wrote:
1./(1./A + 1./B)
will give you answers you can compare to C as e.g.
abs( 1./(1./A + 1./B) - C )
to give an absolute difference of each from C which you can then test against some threshold to see of they are close enough to equal.
  댓글 수: 1
anthony  dall'agnol
anthony dall'agnol 2019년 6월 18일
편집: anthony dall'agnol 2019년 6월 18일
I don't see how doing this
1./(1./A + 1./B)
is going to give me all combinaisons of what I want. Because this operation will just do
for ii = 1:end
someVector = 1/(1/A(ii) + 1/B(ii));
end
And I want something more like
for ii = 1:end
for jj = 1:end
someVector = 1/(1/A(ii) + 1/B(jj));
end
end
and at the end be able to have the index of the value that respect the function. Ideally I would like something like :
[value, index] = nchoosecrit(someVector, @(x,y) function(x,y) == someResult);

댓글을 달려면 로그인하십시오.


Stephen23
Stephen23 2019년 6월 18일
편집: Stephen23 2019년 6월 18일
Simpler to use ndgrid:
>> A = [1561.28, 1561.64, 1562.00, 1562.36, 1562.72]; % signal
>> B = [1064.00, 1064.36, 1064.72, 1065.08, 1065.44]; % pump
>> C = [632.89, 633.02, 633.14, 633.27, 633.40]; % converted
>> [Aa,Ba,Ca] = ndgrid(A,B,C);
>> idx = abs(1./(1./Aa + 1./Ba) - Ca) < 0.01; % adjust tolerance to suit data.
>> [X,Y,Z] = ind2sub(size(idx),find(idx));
>> [X,Y,Z] % indices corresponding to A,B,C:
ans =
3 1 1
1 2 1
3 2 2
1 3 2
5 2 3
3 3 3
5 3 4
3 4 4
3 5 5
And checking (e.g. the last row):
>> 1/( 1/A(3) + 1/B(5) )
ans = 633.40
>> C(5)
ans = 633.40

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by