How to fastly calculate this matrix operation
1 回表示 (過去 30 日間)
古いコメントを表示
A matrix μ is a dimensional matrix, p is a dimensional vector, q is a dimensional matrix, c is a constant.
How to fastly calculate the following three dimensional () matrix X, where the element in X is
4 件のコメント
Torsten
2024 年 7 月 9 日
編集済み: Torsten
2024 年 7 月 9 日
That is, you tell us that q is KxI, but then you index it as q(r,j), where r varies from 1 to K.
It's indexed q(r,i).
Next, the numerator has the shape of a vector, possibly of length either J or I, this is not clear. But the denominator is a 3 dimensional thing.
I think numerator and denominator are just scalars.
John D'Errico
2024 年 7 月 9 日
Sorry. Bad eyesight on my part. it is q(r,i). I thought that was a j. Again, the problem with using I and J. is they are easily confused.
As far as the denominator being a scalar, you can also view it as a 3-dimensional array, of the same shape as X. And I think, as you (@Torsten) knows, that is how you would perform the computation.
採用された回答
John D'Errico
2024 年 7 月 9 日
First, use better variables. I is a terrible name to use , especially capital I, since it is so easily confused with the number 1, and even a lower case L (l). Depending on the font, they can be indistinguishable.
So I will assume that U is a matrix of size Kx1xM. The 1 there is important. And if you have created U to be of size KxN, then you need to reshape it so it is the size I show.
Assume p is a column vector, of length K, so in MATLAB, implicitly of size Kx1x1.
Assume q is an array, of size KxN. In MATLAB, such a 2-d array is also implicitly of size KxNx1.
I'll pick some random arrays.
K = 3;
N = 4;
M = 5; % all arbitrary, just to make an example.
u = rand(K,1,M);
p = rand(K,1,1);
q = rand(K,N,1);
c = randn(); % I'll pick some value for c too
First, how would you compute the denominator?
den = u.^2.*p.*q;
That was trivially done, since MATLAB is smart enough to expand the singleton dimensions into 3-dimensional arrays.
How about the numerator?
num = c + sum(u.^2.*p.*(1-q),1); % the sum will be performed over the first dimension.
Now I would note that num is an array, of size 1xNxM.
X = num./den
The only thing I would question is if you intended the sum to be from 1 to K or from 1 to k. And that would be a difference. We would use cumsum in the latter case.
4 件のコメント
John D'Errico
2024 年 7 月 10 日
NO. Don't copy the columns over one at a tie. USE RESHAPE.
help reshape
u = rand(5,3)
% convert to a 5x1x3
uhat = reshape(u,size(u,1),1,size(u,2))
size(uhat)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!