<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Set submatrix values within a matrix in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Set-submatrix-values-within-a-matrix/m-p/902620#M106164</link>
    <description>&lt;P&gt;I have a matrix of missing values, and I want to set the upper left portion of that matrix to values in another matrix. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;initial_matrix=J(3,3,.);
update_matrix=[1 2, 3 4];
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The desired result is&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;[
1 2 .,
3 4 .,
. . . 
]&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I know I could iterate on the indices of the matrix like this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;
for(i=1, i&amp;lt;=N rows(update_matrix), i++,
	for(k=1, k&amp;lt;=N cols(update_matrix), k++,
		initial_matrix[i,k]=update_matrix[i,k]
	)
);

Show(initial_matrix)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but is there an easier &amp;nbsp;more compact way to do this using direct referencing of the matrix indices?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 22 Sep 2025 15:02:40 GMT</pubDate>
    <dc:creator>MathStatChem</dc:creator>
    <dc:date>2025-09-22T15:02:40Z</dc:date>
    <item>
      <title>Set submatrix values within a matrix</title>
      <link>https://community.jmp.com/t5/Discussions/Set-submatrix-values-within-a-matrix/m-p/902620#M106164</link>
      <description>&lt;P&gt;I have a matrix of missing values, and I want to set the upper left portion of that matrix to values in another matrix. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;initial_matrix=J(3,3,.);
update_matrix=[1 2, 3 4];
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The desired result is&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;[
1 2 .,
3 4 .,
. . . 
]&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I know I could iterate on the indices of the matrix like this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;
for(i=1, i&amp;lt;=N rows(update_matrix), i++,
	for(k=1, k&amp;lt;=N cols(update_matrix), k++,
		initial_matrix[i,k]=update_matrix[i,k]
	)
);

Show(initial_matrix)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but is there an easier &amp;nbsp;more compact way to do this using direct referencing of the matrix indices?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Sep 2025 15:02:40 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Set-submatrix-values-within-a-matrix/m-p/902620#M106164</guid>
      <dc:creator>MathStatChem</dc:creator>
      <dc:date>2025-09-22T15:02:40Z</dc:date>
    </item>
    <item>
      <title>Re: Set submatrix values within a matrix</title>
      <link>https://community.jmp.com/t5/Discussions/Set-submatrix-values-within-a-matrix/m-p/902704#M106166</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/4467"&gt;@MathStatChem&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; This might not be as elegant as what you're hoping for, but it should do it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;initial_matrix = J(3,3,.);
update_matrix=[1 2, 3 4];

initial_matrix[1,1::2]=update_matrix[1,1::2];
initial_matrix[2,1::2]=update_matrix[2,1::2];&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which gives you the following output:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;[1 2 .,
3 4 .,
. . .]&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp; I believe this is what you might be looking for. The indexing goes [row,n1::n2], so [1,1::2] is row 1 elements 1 to 2. The only drawback is that you have to know what submatrix you're assigning the values to ahead of time. If you don't know that information, your loop would be a better approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps!,&lt;/P&gt;
&lt;P&gt;DS&lt;/P&gt;</description>
      <pubDate>Mon, 22 Sep 2025 16:42:00 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Set-submatrix-values-within-a-matrix/m-p/902704#M106166</guid>
      <dc:creator>SDF1</dc:creator>
      <dc:date>2025-09-22T16:42:00Z</dc:date>
    </item>
    <item>
      <title>Re: Set submatrix values within a matrix</title>
      <link>https://community.jmp.com/t5/Discussions/Set-submatrix-values-within-a-matrix/m-p/902716#M106167</link>
      <description>&lt;P&gt;This should work for you.&amp;nbsp; The Try() will simply not assign anything if the update matrix doesn't contain the row and column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
im = J( 3, 3, . );
um = [1 2, 3 4];
im = Transform Each( {e, {r, c}}, im, Try( um[r, c] ) );
////  [1 2 .,
////  3 4 .,
////  . . .]&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 22 Sep 2025 17:14:37 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Set-submatrix-values-within-a-matrix/m-p/902716#M106167</guid>
      <dc:creator>mmarchandFSLR</dc:creator>
      <dc:date>2025-09-22T17:14:37Z</dc:date>
    </item>
    <item>
      <title>Re: Set submatrix values within a matrix</title>
      <link>https://community.jmp.com/t5/Discussions/Set-submatrix-values-within-a-matrix/m-p/902746#M106171</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;m = j(7,8,99);
m[2::5,3::4] = [
	41 42,
	43 44,
	45 46,
	47 48
];
show(m)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;m = 
[	99 99 99 99 99 99 99 99, 
	99 99 41 42 99 99 99 99, 
	99 99 43 44 99 99 99 99, 
	99 99 45 46 99 99 99 99, 
	99 99 47 48 99 99 99 99, 
	99 99 99 99 99 99 99 99, 
	99 99 99 99 99 99 99 99];&lt;/PRE&gt;</description>
      <pubDate>Mon, 22 Sep 2025 19:17:03 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Set-submatrix-values-within-a-matrix/m-p/902746#M106171</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2025-09-22T19:17:03Z</dc:date>
    </item>
    <item>
      <title>Re: Set submatrix values within a matrix</title>
      <link>https://community.jmp.com/t5/Discussions/Set-submatrix-values-within-a-matrix/m-p/902970#M106190</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/982"&gt;@Craige_Hales&lt;/a&gt;&amp;nbsp;, that's what I was looking for. &amp;nbsp;To make it more general, here is what I am doing&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;m=J(7,8,99);

update=[41 42,
	43 44,
	45 46,
	47 48
];

m[1::nrows(update), 1::ncols(update)]=update;
show(m);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;result&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;m = 
[	41 42 99 99 99 99 99 99, 
	43 44 99 99 99 99 99 99, 
	45 46 99 99 99 99 99 99, 
	47 48 99 99 99 99 99 99, 
	99 99 99 99 99 99 99 99, 
	99 99 99 99 99 99 99 99, 
	99 99 99 99 99 99 99 99];&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Sep 2025 13:07:03 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Set-submatrix-values-within-a-matrix/m-p/902970#M106190</guid>
      <dc:creator>MathStatChem</dc:creator>
      <dc:date>2025-09-23T13:07:03Z</dc:date>
    </item>
    <item>
      <title>Re: Set submatrix values within a matrix</title>
      <link>https://community.jmp.com/t5/Discussions/Set-submatrix-values-within-a-matrix/m-p/902971#M106191</link>
      <description>&lt;P&gt;Thanks. &amp;nbsp;I've never used Transform Each( ), look pretty useful, and would not of thought using that on a matrix. &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Sep 2025 13:10:19 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Set-submatrix-values-within-a-matrix/m-p/902971#M106191</guid>
      <dc:creator>MathStatChem</dc:creator>
      <dc:date>2025-09-23T13:10:19Z</dc:date>
    </item>
    <item>
      <title>Re: Set submatrix values within a matrix</title>
      <link>https://community.jmp.com/t5/Discussions/Set-submatrix-values-within-a-matrix/m-p/903331#M106236</link>
      <description>&lt;P&gt;My code should be changed just a bit.&amp;nbsp; The way it's written, any row/column combination not in the update matrix will replace the value in the initial matrix with a missing value.&amp;nbsp; The Try() should have a Catch Expression to prevent overwriting the value, in case the original value isn't missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
im = J( 3, 3, 5 );
um = [1 2, 3 4];
im = Transform Each( {e, {r, c}}, im, Try( um[r, c], e ) );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Sep 2025 11:35:28 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Set-submatrix-values-within-a-matrix/m-p/903331#M106236</guid>
      <dc:creator>mmarchandFSLR</dc:creator>
      <dc:date>2025-09-24T11:35:28Z</dc:date>
    </item>
  </channel>
</rss>

