How to fastly calculate this matrix operation
64 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hancheng Zhu
am 9 Jul. 2024 um 10:52
Kommentiert: Hancheng Zhu
am 11 Jul. 2024 um 2:25
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 Kommentare
Torsten
am 9 Jul. 2024 um 12:30
Bearbeitet: Torsten
am 9 Jul. 2024 um 12:30
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
am 9 Jul. 2024 um 13:24
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.
Akzeptierte Antwort
John D'Errico
am 9 Jul. 2024 um 13:43
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 Kommentare
John D'Errico
am 10 Jul. 2024 um 19:47
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)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!