フィルターのクリア

Sum of Maple to MATLAB

3 ビュー (過去 30 日間)
Bibha
Bibha 2011 年 5 月 14 日
Hi All,
There is a saying "A pee to an ant is flooding".... I guess my question appears simple but being new to MATLAB, couldn't find my way. Seeking some help...
Lm = [1 2 3]';
Um = [4 5 6]';
Maple:
sum(Lm[p,1] * sum(Lm[q,1] * (Um[p,1] - Um[q,1]),q=1..3), p=1..3)
What will be the equivalent MATLAB for the above?
Thank you.
  1 件のコメント
Matt Fig
Matt Fig 2011 年 5 月 14 日
I would have to know what that Maple command did first! Since this is a MATLAB site, it is possible that nobody on here knows what that Maple command does...

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2011 年 5 月 14 日
The most direct equivalent would be
Lm(1,1) * (Lm(2,1) * (Um(1,1) - Um(2,1)) + Lm(3,1) * (Um(1,1) - Um(3,1))) + Lm(2,1) * (Lm(1,1) * (Um(2,1) - Um(1,1)) + Lm(3,1) * (Um(2,1) - Um(3,1))) + Lm(3,1) * (Lm(1,1) * (Um(3,1) - Um(1,1)) + Lm(2,1) * (Um(3,1) - Um(2,1)))
Which works out to be 0
If we generalize to
sum(Lm[p,1] * sum(Lm[q,1] * (Um[p,1] - Um[q,1]),q=1..N), p=1..N) %MAPLE
where N is the length of Um and Lm, then:
breaking it down,
T(p) := sum(Lm[q,1] * (Um[p,1] - Um[q,1]),q=1..N) %Maple
would be
T(p) = sum(Lm .* (Um(p)-Um)) %MATLAB
and when Lm and Um are column vectors, that could be written
T(p) = Lm.' * (Um(p)-Um) %MATLAB
Then,
sum(Lm[p,1] * T[p,1],p=1..N) %Maple
would be
T = zeros(length(Lm),1); %MATLAB
for p=1:T
T(p) = Lm.' * (Um(p)-Um)
end
sum(Lm .* T)
but the sum(Lm .* T) could also be written as Lm.' * T leading to
T = zeros(length(Lm),1); %MATLAB
for p=1:T
T(p) = Lm.' * (Um(p)-Um)
end
Lm.' * T
In turn, in MATLAB that could be written as
Lm.' * arrayfun(@(p) Lm.' * (Um(p)-Um),1:length(Lm)).'
Note here that arrayfun is returning a row vector instead of a column vector, so the .' after the arrayfun is needed to switch back to a column vector for the array multiplication.

その他の回答 (3 件)

Andrei Bobrov
Andrei Bobrov 2011 年 5 月 14 日
more variant
Lm.'*bsxfun(@minus,Um,Um.')*Lm
  2 件のコメント
Walter Roberson
Walter Roberson 2011 年 5 月 14 日
Nice compaction!
Bibha
Bibha 2011 年 5 月 14 日
Thank you again!
Just phenomenal.

サインインしてコメントする。


Matt Fig
Matt Fig 2011 年 5 月 14 日
It would help, for those of us who do not have Maple and consequently do not know what the result of that Maple command is, if you would show the expected output. Even if you have to make a simple example and create the output by hand, show inputs and expected outputs.
The best I can guess would be:
S = 0; % This is the result you want at the end.
for p = 1:3
tmp = 0;
for q = 1:3
tmp = tmp + Lm(q) * (Um(p)-Um(q));
end
S = S + tmp*Lm(p);
end
S % Look at the final result...

Bibha
Bibha 2011 年 5 月 14 日
Thanks for all the answers!
I wish I could select all the answers but I am selecting the one where an educated person is trying to educate other rather than giving the answer right away.
Well, no doubt, I learned more functions from all of you guys but I am giving credit to the hard worker.
Quite amazed by the one line of code that did the trick! Thank you andrei.

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by