Need to improve efficiency in this code, which multiplies a 3D matrix by a 2D one

1 回表示 (過去 30 日間)
Tom
Tom 2015 年 7 月 13 日
コメント済み: Tom 2015 年 7 月 16 日
Hi, I have this code, which uses for loops and multiplies a 3D matrix with a 2D one. It works, but I need f_N to be 3200, and once f_N is that high, it is just too big a calculation for my computer to do. The weird thing is, I have run this code successfully about 10 months ago - I know this because I have a plot generated from it. But I must have changed something maybe.
Anyway, here's the code:
clear all;
num_x = 275;
num_y = 275;
f_N = 100;
W_rsTpre = rand(25,num_x,num_y);
W_rsT(1,:,:,:) = W_rsTpre;
q_pre = rand(25,f_N);
q(:,1,:) = q_pre;
w_pre = zeros(num_x,num_y,1,f_N); % preallocate
for xx = 1:num_x
for yy = 1:num_y
for ff = 1:f_N
w_pre(xx,yy,1,ff) = W_rsT(1,:,xx,yy)*q(:,1,ff);
end
end
end
w = squeeze(w_pre);
Thanks

採用された回答

Andrei Bobrov
Andrei Bobrov 2015 年 7 月 13 日
編集済み: Andrei Bobrov 2015 年 7 月 13 日
W = reshape(W_rsT,size(W_rsT,2),[]).';
q1 = squeeze(q);
w_pre = reshape(W*q1,[num_x, num_y,1,f_N]);
  1 件のコメント
Tom
Tom 2015 年 7 月 16 日
Thank you. Strangely, when I used iequal to see if the two results matched, they did not. But when I plotted them both, they seemed to sit on top of one another - so they must have been pretty much the same.
Many thanks

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by