<div dir="ltr"><div>Thanks,<br><br></div><div>The trick is to create an auxiliar matrix "A" where replics of array "a" fill the columns and to perform regular elemental multiplications with a reshaped version of the target hypermatrix "H" and then reshape the result as original H. It maybe fast because of vectorization but I still have two questions. This demands addtional memory space if one cannot to do everything in one step,<br><pre style="font-style:normal"><span style="font-family:arial,helvetica,sans-serif"><span style="color:rgb(0,0,0)">and, would it be possible to multiply a line vector "b=(1:5)" to all lines of "H" or another array <br></span></span></pre><pre style="font-family:Monospaced;font-style:normal"><span style="color:rgb(0,0,0)"><span style="font-family:arial,helvetica,sans-serif">"c" to the third dimension of H in a vectorized way</span><tt>?<br><br></tt></span></pre><pre style="font-family:Monospaced;font-style:normal"><span style="color:rgb(0,0,0)"><font face="arial,helvetica,sans-serif">Thanks again</font><br></span></pre></div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-05-27 16:19 GMT-03:00 Samuel Gougeon <span dir="ltr"><<a href="mailto:sgougeon@free.fr" target="_blank">sgougeon@free.fr</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<div class="m_-3901716177159843041moz-cite-prefix">Hello Adelson,<span class=""><br>
<br>
Le 27/05/2017 à 17:12, Adelson a écrit :<br>
</span></div><span class="">
<blockquote type="cite">
<pre>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,....)<wbr>*a(:);
And, it seems that in MATLAB one could do something similar using the comand
DOT.</pre>
</blockquote>
<br>
<br></span>
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><span class="HOEnZb"><font color="#888888"><tt>
</tt>
Samuel
</font></span></span></pre>
<br>
</div>
<br>______________________________<wbr>_________________<br>
users mailing list<br>
<a href="mailto:users@lists.scilab.org">users@lists.scilab.org</a><br>
<a href="http://lists.scilab.org/mailman/listinfo/users" rel="noreferrer" target="_blank">http://lists.scilab.org/<wbr>mailman/listinfo/users</a><br>
<br></blockquote></div><br></div>