Triple Summation in Matlab
4 次查看(过去 30 天)
显示 更早的评论
I have the below triple summarion that I need to incorporate into a MATLAB code. Could you please help me with the code of such summation.
F is some factor that depends on a source i, T is a Penalty that depends on the distance for transporation from a source i to a place j, and a is an amount added from i to j in a time period p.
Thanks.
2 个评论
Steven Lord
2019-4-29
What release are you using? Certain techniques that may be useful here will only work in recent releases, so before suggesting them I want to check if you're using an older release.
What does size show for the variables F, a, and T? [I'm assuming they're arrays, not functions that you evaluate with the indices. If that's not a valid assumption, that information will be useful to know.]
回答(1 个)
Steven Lord
2019-4-30
For the first term if F is a column vector and a is a 3-dimensional array that have compatible sizes just multiply them using array multiplication (as opposed to matrix multiplication.)
F = [1; 2; 3]; % size [3 1] (or [3 1 1] or [3 1 1 1] or ...)
a = reshape(1:60, [3 4 5]); % size [3 4 5]
result = F.*a; % size [3 4 5]
Remember as stated on the first of the pages to which I linked, "Every array in MATLAB has trailing dimensions of size 1." Even though F and a have different numbers of dimensions, the implicit trailing dimensions of F mean that they are compatibly sized.
Now sum result over 'all' its dimensions to add together all the values in the result array. When I did this I got a result of 3700 for the example F and a arrays above.
thesum = sum(result, 'all')
If this is for a homework assignment that states you're not allowed to use sum, you can use for loops to iterate through the elements in F and the rows, columns, and pages of a.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!