3D Array Multiplication with 2D Matrix

Hi,
I have a 3D array of lets say 500:10:32 elements. I want to multiple the first dimension of lthis array with another 1:500 matrix. How would I do this?
Regards

2 Kommentare

Bob Thompson
Bob Thompson am 28 Aug. 2019
Which 500 elements from the first matrix do you want to multiply the second matrix by? There are 320 different options.
Or are you looking to multiply all 320 sets of 500 against the new matrix?
Also, how are you looking to store your new 500 x 500 matrices?
Anum Ahmed
Anum Ahmed am 28 Aug. 2019
I am trying to perform the element wise multiplication of the 3D array with a second 2D array and getting the 3D array as a result where lets say row elements would be multiplied with another matrix.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

KALYAN ACHARJYA
KALYAN ACHARJYA am 28 Aug. 2019
Bearbeitet: KALYAN ACHARJYA am 28 Aug. 2019

1 Stimme

data1=rand(500,10,32);% 3D Array
data2=rand(1,500); %1x500 mat
result=data1(1:500,1,1)*data2; %Multiplication

4 Kommentare

Anum Ahmed
Anum Ahmed am 28 Aug. 2019
Hi Kalyan,
As I was trying to perform the element wise multiplication, so I guess the following will work.
data1=rand(500,10,32);% 3D Array
data2=rand(1,500); %1x500 mat
result=data1(1:500,1,1).*data2';
After getting the answer till "result", how am I going to get back the original data1 3D matrix with first dimension point multiplied by data2?
Regards
Bob Thompson
Bob Thompson am 28 Aug. 2019
If you want to replace the original matrix values with your result then you can just use the same indexing that was suggested to indicate the values you want to replace.
data1(:,1,1) = data1(:,1,1).*data2';
KALYAN ACHARJYA
KALYAN ACHARJYA am 28 Aug. 2019
Yes @Bob Thanks
Bob Thompson
Bob Thompson am 28 Aug. 2019
Also, if you want to perform the multiplication on the entire 3D array, then the first thing that comes to mind for me is for loops, or arrayfun. There might be a way to do it without loops, but I don't know it (which doesn't say much).
for i = 1:size(data1,2)
for j = 1:size(data1,3)
data1(:,i,j) = data1(:,i,j).*data2';
end
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Bruno Luong
Bruno Luong am 28 Aug. 2019
Bearbeitet: Bruno Luong am 28 Aug. 2019

1 Stimme

Simply do (R2016b or later)
data1 = data1 .* data2(:)

2 Kommentare

Bob Thompson
Bob Thompson am 28 Aug. 2019
Bearbeitet: Bob Thompson am 28 Aug. 2019
Does this automatically choose the appropriate dimension though? I would logically make the connection between 500 and 500 elements in a dimension, but that doesn't mean a computer will.
Or will it produce 500 500x10x32 arrays, one for each element of data2?
Bruno Luong
Bruno Luong am 28 Aug. 2019
Yes, the length of 500 of the first dimension must match. That's all.

Melden Sie sich an, um zu kommentieren.

stav marzuk
stav marzuk am 7 Dez. 2021

0 Stimmen

Hi i think i have the same problem, but i didnt sucssed to solve it.
I have an rgb photo with the size: 30432 29516 3
and i have a mask bw with the size 30432 29516
is it possible to multiply the two of them?
rgb .*bw
this multplication is not working, is there a solution?
Thanks in advance

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by