COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Matrix element to Matrix element-range mutiplication

• Email
• Follow

```Greetings to you all!

I have two matrices:
Matrix_W=[P,N]
Matrix_X=[4*P,N]

I want to perform a multiplication between the [i,j]-th elements of W and the
[4*i-3 : 4*i , j]-th ranges of elements of X.
and end up with a [4*P,N] matrix.

I there a way to quickly perform such an operation without having to endure the overkill of a double for-loop?

```
 0

See related articles to this posting

```"George Antonopoulos" <georanto@gmail.com> wrote in message
news:htj3cp\$har\$1@fred.mathworks.com...
> Greetings to you all!
>
> I have two matrices:
> Matrix_W=[P,N]
> Matrix_X=[4*P,N]
>
> I want to perform a multiplication between the [i,j]-th elements of W and
> the [4*i-3 : 4*i , j]-th ranges of elements of X.
> and end up with a [4*P,N] matrix.
>
> I there a way to quickly perform such an operation without having to
> endure the overkill of a double for-loop?

Is this what you want?

>> x = [1 2 3;4 5 6];
>> y = rand(8, 3);
>> z = (kron(x, ones(4, 1)).*y)

If not, please post a SMALL example showing specific values for W and X and
what you want the resulting matrix to be.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
http://www.mathworks.com

```
 0

```Is this is what you want?

% Data
P=3;
N=2;
X=ceil(5*rand(4*P,N))
W=ceil(5*rand(P,N))

% Engine
Z=reshape(bsxfun(@times,reshape(W,[1 P N]),reshape(X,[4 P N])),[4*P N])

% Bruno
```
 0

```Both versions seem to provide the result that I need.
I will study them further to see how they work. I had a suspicion
that a kronecker product is involved somewhere.

```
 0