<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">Le 25/04/2017 à 15:29, SCHULZ Wolfgang
a écrit :<br>
</div>
<blockquote
cite="mid:07A8AF089844A549B0F0CCC727AF31AA7545184C@OVEKBG.ove.at"
type="cite">
<pre wrap="">Hello,
I want to set certain elements of a matrix to 1. Interestingly the following code fills 9 elements with 1. I expected to have only 3 elements (with index 1,1; 2,2 and 3,3) filled with 1.
A=zeros(5,3)
inx=[1 2 3];
iny=[1 2 3];
A(inx,iny)=1
Is there any way to fill a matrix with 1 where I have the indexes in 2 vectors?</pre>
</blockquote>
<br>
<pre style="font-family:Monospaced;font-style:normal;font-size:12.0;"><span style="color:rgb(0,0,0);">A</span> <span style="color:rgb(92,92,92);">=</span> <span style="color:rgb(50,185,185);">zeros</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(188,143,143);">5</span><span style="color:rgb(0,0,0);">,</span><span style="color:rgb(188,143,143);">3</span><span style="color:rgb(74,85,219);">)</span>
<span style="color:rgb(0,0,0);">inx</span><span style="color:rgb(92,92,92);"> = </span><span style="color:rgb(74,85,219);">[</span><span style="color:rgb(188,143,143);">1</span> <span style="color:rgb(188,143,143);">2</span> <span style="color:rgb(188,143,143);">3</span><span style="color:rgb(74,85,219);">]</span><span style="color:rgb(0,0,0);">;</span>
<span style="color:rgb(0,0,0);">iny</span><span style="color:rgb(92,92,92);"> = </span><span style="color:rgb(74,85,219);">[</span><span style="color:rgb(188,143,143);">1</span> <span style="color:rgb(188,143,143);">2</span> <span style="color:rgb(188,143,143);">3</span><span style="color:rgb(74,85,219);">]</span><span style="color:rgb(0,0,0);">;</span>
<span style="color:rgb(0,0,0);">A</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(0,0,0);">sub2ind</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(50,185,185);">size</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(0,0,0);">A</span><span style="color:rgb(74,85,219);">)</span><span style="color:rgb(0,0,0);">,</span><span style="color:rgb(0,0,0);">inx</span><span style="color:rgb(0,0,0);">,</span><span style="color:rgb(0,0,0);">iny</span><span style="color:rgb(74,85,219);">)</span><span style="color:rgb(74,85,219);">)</span><span style="color:rgb(92,92,92);"> = </span><span style="color:rgb(188,143,143);">1
</span><tt>--> A(sub2ind(size(A),inx,iny)) = 1</tt><tt>
</tt><tt> A = </tt><tt>
</tt><tt> 1. 0. 0.</tt><tt>
</tt><tt> 0. 1. 0.</tt><tt>
</tt><tt> 0. 0. 1.</tt><tt>
</tt><tt> 0. 0. 0.</tt><tt>
</tt><tt> 0. 0. 0.</tt><tt>
</tt><span style="color:rgb(188,143,143);">
</span></pre>
</body>
</html>