How to fastly calculate this matrix operation
1 view (last 30 days)
Show older comments
Hancheng Zhu
on 9 Jul 2024
Commented: Hancheng Zhu
on 11 Jul 2024
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 Comments
Torsten
on 9 Jul 2024
Edited: Torsten
on 9 Jul 2024
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
on 9 Jul 2024
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.
Accepted Answer
John D'Errico
on 9 Jul 2024
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 Comments
John D'Errico
on 10 Jul 2024
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)
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!