Stuck with simple matrix calculation, can you help?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
So I have two matrixes, both 44x72, let's call them A and V. What I need to do is multiply each value in row/column "ij" by the same value in the other matrix. Then I need to sum all these values up and divide by the sum of all the values in matrix A. Here's another way to think about what I need to do:
Answer = Sum(Aij x Vij) / Sum(Aij) over all i,j grid cells
Sounds easy to do but I can't figure it out. Thanks in advance for the help!
Aaron
採用された回答
Fangjun Jiang
2011 年 8 月 22 日
B=A.*V;
SumA=sum(sum(A));
if SumA~=0
C=sum(sum(B))/SumA;
end
9 件のコメント
Aaron Burdick
2011 年 8 月 22 日
Thanks so much! That worked!
Fangjun Jiang
2011 年 8 月 22 日
I am curious to know the status of your previous question regarding importfile(). Is that a auto-generated function?
James Tursa
2011 年 8 月 22 日
sum(A(:)) and sum(B(:)) are a bit cleaner than sum(sum(A)) and sum(sum(B))
Fangjun Jiang
2011 年 8 月 23 日
Good point! Will keep that in mind!
Sean de Wolski
2011 年 8 月 23 日
@James, sadly not always (or even usually) faster.
Fangjun Jiang
2011 年 8 月 23 日
@Sean, How do we know which method is more efficient in general? I mean, it shouldn't require a test case every time. Is there any documentation guidance?
Fangjun Jiang
2011 年 8 月 23 日
@Sean, How do we know which method is more efficient in general? I mean, it shouldn't require a test case every time. Is there any documentation guidance?
Sean de Wolski
2011 年 8 月 23 日
No, it can't easily be documented because it's system and version dependent: yours will surely be different than mine. This happened with Matt Fig and me on a question a few months ago. I had a vectorized solution that used big matrices, he used a well designed for-loop. Mine was much faster on my system, his was faster on his.
As for the (:), here are three methods for doing the sum. On mine the sum(sum()) is the fastest followed by reshape (with the numel(), reshape is slowed to the speed of a drunk turtle with a []) and then colon comes in third. I think the newer versions have a faster colon (<humor!>).
A = magic(1000);
[t1 t2 t3] = deal(0);
for ii = 1:100
tic
B = sum(sum(A));
t1 = t1+toc;
tic
C = sum(A(:));
t2 = t2+toc;
tic
D = sum(reshape(A,numel(A),1));
t3 = t3+toc;
end
[t1 t2 t3]
When I make A have more dimensions, say 5, both reshape and colon are faster. But for 2d stuff, sum(sum is fair.
Fangjun Jiang
2011 年 8 月 23 日
Thanks Sean for the insight!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
参考
2011 年 8 月 22 日
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
