I want to know how to multiply array elements one by one.

Hello, I have some problems with making matlab code.
If I have 2x2 matrix. I want to calculate (1,1)x(1,1), (1,1)x(1,2) (1,1)x(2,1), (1,1)x(2,2)
(1,2)x(1,2), (1,2)x(2,1), (1,2)x(2,2) -> (1,2)x(1,1) is already exists in the first row.
(2,1)x(2,1), (2,1)x(2,2) -> (2,1)x(1,1), (2,1)x(1,2) are already existed in the previous rows.
(2,2) values.
(1,1) is the element of matrix value. for example) if matrix [1,2,3;4,5,6;7,8,9], then (1,1) means 1.
So, what I want to say is (1,1)x(1,2) = 1x2=2.
I want to expand this to m x m matrix.
how I make the code or which function should I use?
Until now I have used just ' for '.
I made the code with 4 parameter (ex) i,j,k,l)
However, this operation has duplicate values, and as the matrix becomes large, the operation speed becomes significantly longer.
plz help me....

7 Comments

(1,1)x(1,1)
x(1,1) is the element from first row and first column. What is (1,1) before to x(1,1)?
(1,1) is the element of matrix value. for example) if matrix [1,2,3;4,5,6;7,8,9], then (1,1) means 1.
So, what I want to say is (1,1)x(1,2) = 1x2=2.
Can you show the expected result for the matrix
[1,2,3;4,5,6;7,8,9]
and the way you want to save this result ?
Calculating as I intended gives a total of 45 results, so I'll collapse the matrix to [1,2;3,4].
Then, (1,1)x(1,1) = 1x1 = 1, (1,1)x(1,2) =1x2 = 2, (1,1)x(2,1) = 1x3 = 3, (1,1)x(2,2) = 1x4=4
(1,2)x(1,2) = 2x2 = 4, (1,2)x(2,1) = 2x3 = 6, (1,2)x(2,2) = 2x4 = 8 .
(2,1)x(2,1) = 3x3 =9 , (2,1)x(2,2) = 3x4 = 12
(2,2)x(2,2) = 4x4 = 16.
And I will save this result to the n x 2 matrix. In the above case, n=10.
If I use 3x3 matrix then, n=45.
The first column is the distance. That means, if I calculate (1,2)x(2,2), then the distance is the sqrt((2-1)^2+(2-2)^2). if i calculate (1,2)x(3,4), then the distance is the sqrt(3-1)^2+(4-2)^2).
and the second column is the calculated value. That means if I calculate (1,2)x(2,2), then the value is 12.
That means if I calculate (1,2)x(2,2), then the value is 12.
I thought the value was 8.
1st column is for the distance between two matrix elements.
2nd column is for the product of two elements.
if I calculate (1,2)x(2,2), then the distance is the sqrt((2-1)^2+(2-2)^2).
the second column is the calculated value. That means if I calculate (1,2)x(2,2), then the value is 12.
oh sorry! you are right. I missed. (1,2)x(2,2) is 2x4 = 8.

Sign in to comment.

Answers (1)

A=[1 2; 3 4];
B1=A(1,1)*A(:);
B2=A(1,2)*A(:);
B3=A(2,1)*A(:);
B4=A(2,2)*A(:);
matrix=[B1 B2 B3 B4];
out=reshape(matrix,1,[]);
out2=unique(out)
out2 = 1×9
1 2 3 4 6 8 9 12 16
And I will save this result to the n x 2 matrix. In the above case, n=10.
no, its 9 element row vector. so you can not reshaped into n X 2 arrray.

5 Comments

A=[1 2; 3 4];
C=cell(size(A,2),1);
for j=1:size(A,2)
for k=1:size(A,2)
C{j,k}=A(j,k)*A(:);
end
end
matrix=[C{:}];
out=reshape(matrix,1,[]);
out2=unique(out)
out2 = 1×9
1 2 3 4 6 8 9 12 16
Why the "unique(out)" ?
The OP only wanted to point out that some elements are calculated several times and how this could be avoided for the sake of efficiency.
Thanks for your reply.
In this case, I will not use the unique function. Ironically, in the final results(In your code, it is the 'out' variable.), duplicate values are important for me.
The duplicate values ​​I said in the question mean things like (1,2)x(2,1) and (2,1)x(1,2).
And I will use the more big matrix, for example, minimum 100 x 100 to maximum 10000x10000?.
But your answer is very helpful for me.
thank you!
B2's A(1,2)*A(2,1) and B3's A(2,1)*A(1,2) are duplicate values. I don't need this operation.
But, A(1,1)xA(2,2) = 1x4=4, A(1,2)xA(1,2) = 2x2 = 4 is different from the above.
The redundancy of the multiplication is unnecessary, but the redundancy of the result is important.
So, what i want to say is I want to avoid redundancy in multiplication.

Sign in to comment.

Categories

Asked:

on 15 Mar 2022

Commented:

on 15 Mar 2022

Community Treasure Hunt

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

Start Hunting!