the description below .. for loop and matrices

% when o=1 I want to multiply the second row of H with the second column of W and sum it with the multiplication of the third row of H with the third column of W and the multiplication of the fourth row of H with the fourth column of W.
% when o=2 I want to multiply the first row of H with the first column of W and sum it with the multiplication of the third row of H with the third column of W and the multiplication of the fourth row of H with the fourth column of W.
% when o=3 I want to multiply the first row of H with the first column of W and sum it with the multiplication of the second row of H with the second column of W and the multiplication of the fourth row of H with the fourth column of W.
% and so on .....
clc;
clear;
H=[1 2 2 1 ; 3 1 1 2;1 3 2 4;2 1 3 5]
W=[4 1 2 1;1 3 2 1;2 1 1 3;2 1 1 4]
for o=1:4
p = abs(H(o,:)*W(:,o))
end

 Respuesta aceptada

H=[1 2 2 1 ; 3 1 1 2;1 3 2 4;2 1 3 5]
H = 4×4
1 2 2 1 3 1 1 2 1 3 2 4 2 1 3 5
W=[4 1 2 1;1 3 2 1;2 1 1 3;2 1 1 4]
W = 4×4
4 1 2 1 1 3 2 1 2 1 1 3 2 1 1 4
locs = [2 2 3 3 4 4;
1 1 3 3 4 4;
1 1 2 2 4 4];
for o = 1 : size(locs,1)
p(o) = H(locs(o,1),:)*W(:,locs(o,2)) + H(locs(o,3),:)*W(:,locs(o,4)) + H(locs(o,5),:)*W(:,locs(o,6));
end
p
p = 1×3
55 58 53

6 comentarios

Matthew Worker
Matthew Worker el 26 de Jun. de 2021
Editada: Leo Map el 26 de Jun. de 2021
What you did is correct but I want it to any number of iteration and any H,W dimension. o is the number of rows of H and the number of columns of W so if the H is 5xA the W is Fx5 and o = 5.
I give you 5 as an example but I want it for any number.
H=[1 2 2 1 ; 3 1 1 2;1 3 2 4;2 1 3 5]
H = 4×4
1 2 2 1 3 1 1 2 1 3 2 4 2 1 3 5
W=[4 1 2 1;1 3 2 1;2 1 1 3;2 1 1 4]
W = 4×4
4 1 2 1 1 3 2 1 2 1 1 3 2 1 1 4
locs = [2 2 3 3 4 4;
1 1 3 3 4 4;
1 1 2 2 4 4];
for o = 1 : size(H,1)
p(o) = H(locs(o,1),:)*W(:,locs(o,2)) + H(locs(o,3),:)*W(:,locs(o,4)) + H(locs(o,5),:)*W(:,locs(o,6));
end
Index in position 1 exceeds array bounds (must not exceed 3).
p
Computers are very poor at know what "% and so on ....." means.
The instructions you posted are consistent with the possibility that you want to use the same row of H as you use column of W. If so then the table of locations can be cut in half, but I did not want to presume that using the same row H = column W was always going to be true.
The instructions you posted have no logic about which rows and columns are to be used, so the location table must be built manually.
There is the possibility that what you meant to describe was that for o = 1 you want all of the row/column except H row 1 / W column 1, and that for o = 2 you want all of the row / column except H row 2 / W column 2, and so on... that each time you want everything except diagonal. If so then the easiest way would be to compute the total and subtract off the individuals. For example,
parts = diag(H*W);
sum(parts) - parts
H=[1 2 2 1 ; 3 1 1 2;1 3 2 4;2 1 3 5]
H = 4×4
1 2 2 1 3 1 1 2 1 3 2 4 2 1 3 5
W=[4 1 2 1;1 3 2 1;2 1 1 3;2 1 1 4]
W = 4×4
4 1 2 1 1 3 2 1 2 1 1 3 2 1 1 4
parts = diag(H*W);
sum(parts) - parts
ans = 4×1
55 58 53 35
Matthew Worker
Matthew Worker el 27 de Jun. de 2021
I think I describe the problem more clearly in this link.
Thank you.
Suppose for discussion that H is 4 x 3, and W is 5 x 4
H=[1 2 2; 3 1 1;1 3 2;2 1 3]
H = 4×3
1 2 2 3 1 1 1 3 2 2 1 3
W=[4 1 2 1;1 3 2 1;2 1 1 3;2 1 1 4; 1 2 4 5]
W = 5×4
4 1 2 1 1 3 2 1 2 1 1 3 2 1 1 4 1 2 4 5
Now for the first iteration, you define
o = 1
p = abs(H(o,:)*W(:,o))
H(o,:) is 1 x 3 and W(:,o) is 5 x 1. But you cannot use * between a 1 x 3 and a 5 x 1.
Your definitions cannot work unless you define that W has the same number of rows that H has columns -- which you do not do.
If you had defined
p = abs(W(:,o)*H(o,:))
then that would work, giving a 5 x 3 result.
Which do you want? That each entry should be size(W,1) x size(H,2) ? Or that each entry should be 1 x 1, in which case you would have to define H as having the same number of columns as W has rows.
Matthew Worker
Matthew Worker el 27 de Jun. de 2021
Editada: Leo Map el 27 de Jun. de 2021
you are right, W has the same number of rows that H has columns,but ... when (i=1) I need the multiplication of the first row of H and the first column of W and put it in variable Alfa_1 and then multiplying separately the second row of H and the second column of W and sum it with the multiplication of the third row of H and the third column of W and sum it with the multiplication of the fourth row of H and the fourth column of W and put the sum in Alfa_2.
... when (i=2) I need the multiplication of the second row of H and the second column of W and put it in variable Alfa_1 and then multiplying separately the first row of H and the first column of W and sum it with the multiplication of the third row of H and the third column of W and sum it with the multiplication of the fourth row of H and the fourth column of W and put the sum in Alfa_2.
and goes like this.
I'm sorry because I didn't describe clearly what I need.
Thank you

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 26 de Jun. de 2021

Comentada:

el 16 de Dic. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by