How can I multiply each row of 3 matrices individually?

5 次查看(过去 30 天)
Hello everyone,
I have 3 matrixes to be multiplied. x[36,36],r[36,36],a[36x1].
What i wanna do is this;
x(1,1)*r(1,1)*ai(1)+x(2,1)*r(2,1)*ai(2)+....+x(35,1)*r(35,1)*ai(35)+x(36,1)*r(36,1)*ai(36) <=Kj*y(1)
x(1,2)*r(1,2)*ai(1)+x(2,2)*r(2,2)*ai(2)+....+x(35,2)*r(35,2)*ai(35)+x(36,2)*r(36,2)*ai(36)<=Kj*y(2)
...
x(1,36)*r(1,36)*ai(1)+x(2,36)*r(2,36)*ai(2)+....+x(35,36)*r(35,36)*ai(35)+x(36,36)*r(36,36)*ai(36)<=Kj*y(36)
This is what i wrote so far;
sum(x.*rij*ai,2)- Kj*y(:,1)<=0
this does what i want to do with the right side( Kj*y()) but the left side is the opposite what i want.
it does this;
x(1,1)*r(1,1)*ai(1)+x(1,2)*r(1,2)*ai(2)+....+x(1,35)*r(1,35)*ai(35)+x(1,36)*r(1,36)*ai(36)<=Kj*y(1)
hope this is clear. open to every suggestions. What i am looking for is only one line! Like the one i tried writing!
Thank you in advance!

回答(3 个)

David Hill
David Hill 2022-4-11
sum(x.*rij*ai)- Kj*y(:,1)<=0;%want to add columns (delete ,2)
  3 个评论
Azime Beyza Ari
Azime Beyza Ari 2022-4-11
Oh, sorry!
Already tried that. But what i want is row multpilication only. i have 36 rows for x and r. but when i do this ;
sum(x.*rij*ai)
it just multiplies and sums all of the matrix. And it does this for 36 times. So not what i am looking for.

请先登录,再进行评论。


Star Strider
Star Strider 2022-4-11
编辑:Star Strider 2022-4-11
The part of this involving K and y is ambiguous.
It would help to know what and y are, because it appears that Kis a vector, and y is a matrix, the columns of which are used to multiply K to test the inequality.
x = randi(9,4)
x = 4×4
1 9 6 3 6 5 3 2 7 9 9 6 8 8 9 6
r = randi(9,4)
r = 4×4
7 8 6 9 6 9 5 9 9 8 6 9 2 3 5 4
a = randi(9,4,1)
a = 4×1
9 9 7 4
z = (x .* r)' .* a
z = 4×4
63 324 567 144 648 405 648 216 252 105 378 315 108 72 216 96
z_sum = sum(z,2)
z_sum = 4×1
1098 1917 1050 492
K = 92;
y = randi(9,1,4)
y = 1×4
9 2 6 4
Ky = K .* y
Ky = 1×4
828 184 552 368
z_logical = z_sum <= Ky
z_logical = 4×4 logical array
0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
z_logical = sum((x .* r)' .* a, 2) <= Ky % Single-Line Version
z_logical = 4×4 logical array
0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
for k1 = 1:size(x,1) % This Checks To Be Certain That The 'z' Matrix Is Caclulated Correctly
for k2 = 1:size(x,2)
z(k2,k1) = x(k2,k1) * r(k2,k1) * a(k2); % Check
end
end
z
z = 4×4
63 648 324 243 324 405 135 162 441 504 378 378 64 96 180 96
EDIT — (11 Apr 2022 at 17:48)
Changed ‘K’ to be a constant scalar, and ‘y’ to be a row vector.
Since ‘z_sum’ is a column vector, the result of the logical comparison with ‘Ky’ will be a logical matrix. If optimtool wants a scalar result, it will be necessary to do further processing on ‘z_logical’.
.
  1 个评论
Azime Beyza Ari
Azime Beyza Ari 2022-4-11
sorry for unclear information. K is a 92. y has dimensions (1x36).
Also thank you for your answer. But what i am looking for is just only one line. I am using optimtool and trying to write it on only one line.

请先登录,再进行评论。


Torsten
Torsten 2022-4-11
编辑:Torsten 2022-4-11
(x.*rij).' * ai - Kj*y <= 0
assuming that ai and y are column vectors.
  2 个评论
Azime Beyza Ari
Azime Beyza Ari 2022-4-11
Yeah did not worked. Gives error saying; Incorrect dimensions for matrix multiplication. check if the no of columns in first matrixx matches the no of rows in the second matrix.
but they match.
Torsten
Torsten 2022-4-11
编辑:Torsten 2022-4-11
Is y a vector or a matrix ?
Because you wrote y(:,1) instead of y above.
Or do you have to specify the "0" of the right-hand side as "zeros(size(y,1),1)" ?
Try also
(x.*rij)'*ai - Kj*y <= 0

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by