フィルターのクリア

Multiply matrices of different sizes

3 ビュー (過去 30 日間)
NMans
NMans 2018 年 3 月 14 日
コメント済み: Jan 2018 年 3 月 19 日
Background: I've got 7 years worth of Power data for a wind farm binned into 100 bins of wind speed i.e. 100 x 7 matrix. I've got probability of each of that bin happening i.e. 100 x 1 matrix. I would like to multiply the elements of 100x7 matrix by elements in a 100x1 matrix to get cumulative probability of power output.
In simple term: how do I multiply 100x7 matrix by 100x 1 matrix? Tried bsxfun but didn't work.
  2 件のコメント
Jan
Jan 2018 年 3 月 16 日
"Didn't work" is a weak explanation. Better post your code and the complete error message. Not that bsxfun will be the solution, so letting us guess, what fails in your case, is not useful.
NMans
NMans 2018 年 3 月 19 日
編集済み: NMans 2018 年 3 月 19 日
I've got Pow_ave which is 100x7 matrix i.e. 7 years of average power data and prob_bins which is probability of wind speed occuring from hiscounts i.e. 100X1 matrix. My codes are as below:
for yrs = 1:length(years)
[N,edges,bin] = histcounts(SPEED(:,yrs),100);
COUNT = [N]';
nelements = sum(COUNT);
prob_bins = COUNT./nelements;
Area = bsxfun(@times, Pow_ave(:,yrs), prob_bins)
end
The results should be 100x7 matrix but I've got 100x1 matrix instead.
I could plot Pow_ave vs prob_bins and find area under graph but thought this is a simpler method.

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

採用された回答

Jan
Jan 2018 年 3 月 19 日
編集済み: Jan 2018 年 3 月 19 日
Your code:
for yrs = 1:length(years)
[N,edges,bin] = histcounts(SPEED(:,yrs),100);
COUNT = N.'; % Here [] is a waste of time
nelements = sum(COUNT);
prob_bins = COUNT ./ nelements;
Area = bsxfun(@times, Pow_ave(:,yrs), prob_bins);
end
Area is overwritten in each iteratotion. Maybe you want:
Area(:, yrs) = Pow_ave(:,yrs) .* prob_bins;
As far as I can see, Pow_ave(:, yrs) and prob_bins are both 100x1 vectors, so there is no need for bsxfun.
  2 件のコメント
NMans
NMans 2018 年 3 月 19 日
Thank you! This works now - I just have to add (:,yrs) after Area.
Jan
Jan 2018 年 3 月 19 日
@NMans: Pre-allocate theoutput before the loop:
Area = zeros(100, 7);
This avoids the iterative growing of the array, which is expensive in general - although the runtime will not matter much in this case.

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

その他の回答 (1 件)

Geoff Hayes
Geoff Hayes 2018 年 3 月 14 日
NMans - if you want to multiply each column of the 100x7 matrix by the 100x1 matrix, then you could use bsxfun as
X = randi(255,100,7); % generate some dummy data
Y = rand(100,1);
Z = bsxfun(@times,X,Y);
Or are you trying to do something else?
  3 件のコメント
Geoff Hayes
Geoff Hayes 2018 年 3 月 16 日
NMans - when I run the code that I posted in my answer, Z is a 100x7 matrix. Are you sure that your answer is 100x100?
NMans
NMans 2018 年 3 月 19 日
編集済み: Walter Roberson 2018 年 3 月 19 日
My codes are as below:
for yrs = 1:length(years)
[N,edges,bin] = histcounts(SPEED(:,yrs),bins);
COUNT = [N]';
nelements = sum(COUNT);
prob_bins = COUNT./nelements;
Area = bsxfun(@times, Pow_ave(:,yrs), prob_bins);
end
And I ended up with 100x1 matrix.

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

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by