creating algorithm for specific computation

조회 수: 3 (최근 30일)
sermet
sermet 2018년 9월 19일
댓글: Guillaume 2018년 9월 19일
I need to perform below computation using algorithm approach.
t=100; % constant et=1:1:4; % the numbers of "et" are variable.
result=(((t-et(2))*(t-et(3))*(t-et(4)))/((et(1)-et(2))*(et(1)-et(3))*(et(1)-et(4))))+(((t-et(1))*(t-et(3))*(t-et(4)))/((et(2)-et(1))*(et(2)-et(3))*(et(2)-et(4))));
How can I create algorithm to perform above computation automatically with respect to the array number of et?

채택된 답변

Guillaume
Guillaume 2018년 9월 19일
If I understood correctly:
numerators = t - repmat(et(:), 1, numel(et)); %calculate t-et(1), t-et(2), etc.
numerators(logical(eye(size(numerators)))) = 1; %replace t-et by 1 on diagonal
numerators = prod(numerators);
denominators = et - et';
denominators(logical(eye(size(denominators)))) = 1;
denominators = prod(denominators);
result = sum(numerators ./ denominators);
However, the result of this is always going to be 1. So
result = 1;
may be simpler.
  댓글 수: 2
sermet
sermet 2018년 9월 19일
편집: sermet 2018년 9월 19일
I got "Matrix dimensions must agree" error for denominators = et - et'; line
Guillaume
Guillaume 2018년 9월 19일
That would be because you're using an ancient version of matlab (< R2016b). In older versions:
denominators = bsxfun(@minus, et, et');

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by