How to fastly calculate this matrix operation

조회 수: 1 (최근 30일)
Hancheng Zhu
Hancheng Zhu 2024년 7월 9일
댓글: Hancheng Zhu 2024년 7월 11일
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
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
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
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
X =
X(:,:,1) = 1.0e+03 * 2.5730 0.3042 0.2057 0.3599 0.0013 0.0115 0.0181 0.0079 0.0002 0.0010 0.0015 0.0046 X(:,:,2) = 192.3316 24.7216 16.0489 25.4394 0.6655 6.5974 9.9161 3.9023 0.2438 1.1346 1.6771 4.6834 X(:,:,3) = 96.8484 14.0472 9.1383 15.2627 0.5831 6.5235 9.8254 4.0742 0.1876 0.9850 1.4589 4.2928 X(:,:,4) = -781.9146 -26.1164 -13.5545 -15.1076 -14.9784 -38.5860 -46.3653 -12.8301 -2.0081 -2.4281 -2.8693 -5.6342 X(:,:,5) = 49.6421 1.8893 0.7914 2.0345 42.2240 123.9404 120.2054 76.7182 0.7737 1.0659 1.0167 4.6044
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
John D'Errico 2024년 7월 10일
NO. Don't copy the columns over one at a tie. USE RESHAPE.
help reshape
RESHAPE Reshape array by rearranging existing elements B = RESHAPE(A,M,N) or RESHAPE(A,[M,N]) returns the M-by-N matrix whose elements are taken columnwise from A. A must have M*N elements. B = RESHAPE(A,M,N,P,...) or RESHAPE(A,[M,N,P,...]) returns a multidimensional array with the same elements as A but reshaped to have the size M-by-N-by-P-by-.... The product of the specified dimensions, M*N*P*..., must be the same as NUMEL(A). B = RESHAPE(A,...,[],...) reshapes A according to the specified dimension sizes. You can specify a single dimension size of [] to have the dimension size automatically calculated, such that the product of the dimensions equals NUMEL(A). The value of NUMEL(A) must be evenly divisible by the product of the specified dimensions. See also SQUEEZE, SHIFTDIM, COLON, RESIZE. Documentation for reshape doc reshape Other uses of reshape categorical/reshape codistributed/reshape datetime/reshape digraph/reshape dlarray/reshape duration/reshape gpuArray/reshape graph/reshape InputOutputModel/reshape lib.pointer/reshape matlab.mixin.indexing.RedefinesParen/reshape quaternion/reshape RandStream/reshape se2/reshape se3/reshape so2/reshape so3/reshape sym/reshape symbolic/reshape tabular/reshape tall/reshape tokenizedDocument/reshape
u = rand(5,3)
u = 5x3
0.2219 0.7364 0.4874 0.3214 0.6192 0.0274 0.4354 0.0881 0.8771 0.0893 0.0874 0.4635 0.3561 0.9840 0.7098
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% convert to a 5x1x3
uhat = reshape(u,size(u,1),1,size(u,2))
uhat =
uhat(:,:,1) = 0.2219 0.3214 0.4354 0.0893 0.3561 uhat(:,:,2) = 0.7364 0.6192 0.0881 0.0874 0.9840 uhat(:,:,3) = 0.4874 0.0274 0.8771 0.4635 0.7098
size(uhat)
ans = 1x3
5 1 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Hancheng Zhu
Hancheng Zhu 2024년 7월 11일
Thanks bro!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by