<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">Hello Adelson,<br>
<br>
Le 27/05/2017 à 17:12, Adelson a écrit :<br>
</div>
<blockquote cite="mid:1495897957822-4036460.post@n3.nabble.com"
type="cite">
<pre wrap="">Hello,
How to obtain the product of a one dimensional array and a hypermatrix in a
vector way?
I mean, an array a(1:N) times a hypermatrix H(1:M,1:N,1:P,....), for all
m,p,q,... indices:
for m=1:M
for p=1:P
for q=.....
....
R(m,:,p,q,....) =H(m,:,p,q,...) .* a(:);
....
end
end
end
in a vectorized form?
As an example, in FORTRAN one could simply write in a vectorized form,
FORALL(m=1:M,p=1:P,q=...,...); R(m,:,p,q,...)=H(m,:,p,q,....)*a(:);
And, it seems that in MATLAB one could do something similar using the comand
DOT.</pre>
</blockquote>
<br>
<br>
You can use<br>
<pre style="font-family:Monospaced;font-style:normal;font-size:12.0;"><tt><span style="color:rgb(0,0,0);">tmp</span></tt><tt> </tt><tt><span style="color:rgb(92,92,92);">=</span></tt><tt> </tt><tt><span style="color:rgb(50,185,185);">matrix</span></tt><tt><span style="color:rgb(74,85,219);">(</span></tt><tt><span style="color:rgb(0,0,0);">H</span></tt><tt><span style="color:rgb(0,0,0);">,</span></tt><tt><span style="color:rgb(50,185,185);">size</span></tt><tt><span style="color:rgb(74,85,219);">(</span></tt><tt><span style="color:rgb(0,0,0);">H</span></tt><tt><span style="color:rgb(0,0,0);">,</span></tt><tt><span style="color:rgb(188,143,143);">1</span></tt><tt><span style="color:rgb(74,85,219);">)</span></tt><tt><span style="color:rgb(0,0,0);">,</span></tt><tt><span style="color:rgb(92,92,92);">-</span></tt><tt><span style="color:rgb(188,143,143);">1</span></tt><tt><span style="color:rgb(74,85,219);">)</span></tt><tt><span style="color:rgb(0,0,0);">;</span></tt><tt>
</tt><tt><span style="color:rgb(0,0,0);">R</span></tt><tt> </tt><tt><span style="color:rgb(92,92,92);">=</span></tt><tt> </tt><tt><span style="color:rgb(50,185,185);">matrix</span></tt><tt><span style="color:rgb(74,85,219);">(</span></tt><tt><span style="color:rgb(0,0,0);">tmp</span></tt><tt> </tt><tt><span style="color:rgb(92,92,92);">.*</span></tt><tt> </tt><tt><span style="color:rgb(74,85,219);">(</span></tt><tt><span style="color:rgb(0,0,0);">a</span></tt><tt><span style="color:rgb(74,85,219);">(</span></tt><tt><span style="color:rgb(255,170,0);">:</span></tt><tt><span style="color:rgb(74,85,219);">)</span></tt><tt> </tt><tt><span style="color:rgb(92,92,92);">*</span></tt><tt> </tt><tt><span style="color:rgb(50,185,185);">ones</span></tt><tt><span style="color:rgb(74,85,219);">(</span></tt><tt><span style="color:rgb(0,0,0);">tmp</span></tt><tt><span style="color:rgb(74,85,219);">(</span></tt><tt><span style="color:rgb(188,143,143);">1</span></tt><tt><span style="color:rgb(0,0,0);">,</span></tt><tt><span style="color:rgb(255,170,0);">:</span></tt><tt><span style="color:rgb(74,85,219);">)</span></tt><tt><span style="color:rgb(74,85,219);">)</span></tt><tt><span style="color:rgb(74,85,219);">)</span></tt><tt><span style="color:rgb(0,0,0);">,</span></tt><tt> </tt><tt><span style="color:rgb(50,185,185);">size</span></tt><tt><span style="color:rgb(74,85,219);">(</span></tt><tt><span style="color:rgb(0,0,0);">H</span></tt><tt><span style="color:rgb(74,85,219);">)</span></tt><tt><span style="color:rgb(74,85,219);">)</span></tt><span style="color:rgb(0,0,0);"><tt>;</tt><tt>
</tt>
</span>Example:
<span style="color:rgb(0,0,0);"><tt>--> H = grand(3,5,2,2,"uin",0,2)</tt><tt>
</tt><tt> H = </tt><tt>
</tt><tt>(:,:,1,1)</tt><tt>
</tt><tt> 2. 2. 2. 1. 0.</tt><tt>
</tt><tt> 0. 1. 2. 1. 0.</tt><tt>
</tt><tt> 2. 1. 0. 2. 2.</tt><tt>
</tt><tt>(:,:,2,1)</tt><tt>
</tt><tt> 0. 1. 1. 0. 0.</tt><tt>
</tt><tt> 0. 1. 2. 2. 1.</tt><tt>
</tt><tt> 1. 2. 1. 1. 2.</tt><tt>
</tt><tt>(:,:,1,2)</tt><tt>
</tt><tt> 0. 2. 1. 0. 2.</tt><tt>
</tt><tt> 0. 0. 2. 0. 0.</tt><tt>
</tt><tt> 1. 1. 1. 0. 2.</tt><tt>
</tt><tt>(:,:,2,2)</tt><tt>
</tt><tt> 0. 1. 2. 0. 2.</tt><tt>
</tt><tt> 0. 2. 2. 0. 1.</tt><tt>
</tt><tt> 1. 1. 0. 1. 2.</tt><tt>
</tt><tt>
</tt><tt>--> a = (1:3)'</tt><tt>
</tt><tt> a = </tt><tt>
</tt><tt> 1.</tt><tt>
</tt><tt> 2.</tt><tt>
</tt><tt> 3.</tt><tt>
</tt><tt>
</tt><tt>--> tmp = matrix(H,size(H,1),-1);</tt><tt>
</tt><tt>--> R = matrix(tmp .* (a(:) * ones(tmp(1,:))), size(H));</tt><tt>
</tt><tt>--> R</tt><tt>
</tt><tt> R = </tt><tt>
</tt><tt>(:,:,1,1)</tt><tt>
</tt><tt> 2. 2. 2. 1. 0.</tt><tt>
</tt><tt> 0. 2. 4. 2. 0.</tt><tt>
</tt><tt> 6. 3. 0. 6. 6.</tt><tt>
</tt><tt>(:,:,2,1)</tt><tt>
</tt><tt> 0. 1. 1. 0. 0.</tt><tt>
</tt><tt> 0. 2. 4. 4. 2.</tt><tt>
</tt><tt> 3. 6. 3. 3. 6.</tt><tt>
</tt><tt>(:,:,1,2)</tt><tt>
</tt><tt> 0. 2. 1. 0. 2.</tt><tt>
</tt><tt> 0. 0. 4. 0. 0.</tt><tt>
</tt><tt> 3. 3. 3. 0. 6.</tt><tt>
</tt><tt>(:,:,2,2)</tt><tt>
</tt><tt> 0. 1. 2. 0. 2.</tt><tt>
</tt><tt> 0. 4. 4. 0. 2.</tt><tt>
</tt><tt> 3. 3. 0. 3. 6.</tt><tt>
</tt>
Samuel
</span></pre>
<br>
</body>
</html>