cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • JMP 19 is here! See the new features at jmp.com/new.
  • Register to attend Discovery Summit 2025 Online: Early Users Edition, Sept. 24-25.
Choose Language Hide Translation Bar
MathStatChem
Level VII

Set submatrix values within a matrix

I have a matrix of missing values, and I want to set the upper left portion of that matrix to values in another matrix.  

 

initial_matrix=J(3,3,.);
update_matrix=[1 2, 3 4];

The desired result is

[
1 2 .,
3 4 .,
. . . 
]

I know I could iterate on the indices of the matrix like this


for(i=1, i<=N rows(update_matrix), i++,
	for(k=1, k<=N cols(update_matrix), k++,
		initial_matrix[i,k]=update_matrix[i,k]
	)
);

Show(initial_matrix)

but is there an easier  more compact way to do this using direct referencing of the matrix indices?

 

 

2 ACCEPTED SOLUTIONS

Accepted Solutions

Re: Set submatrix values within a matrix

This should work for you.  The Try() will simply not assign anything if the update matrix doesn't contain the row and column.

 

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 .,
////  . . .]

View solution in original post

Craige_Hales
Super User

Re: Set submatrix values within a matrix

m = j(7,8,99);
m[2::5,3::4] = [
	41 42,
	43 44,
	45 46,
	47 48
];
show(m)
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];
Craige

View solution in original post

5 REPLIES 5
SDF1
Super User

Re: Set submatrix values within a matrix

Hi @MathStatChem ,

 

  This might not be as elegant as what you're hoping for, but it should do it:

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];

Which gives you the following output:

[1 2 .,
3 4 .,
. . .] 

  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.

 

Hope this helps!,

DS

Re: Set submatrix values within a matrix

This should work for you.  The Try() will simply not assign anything if the update matrix doesn't contain the row and column.

 

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 .,
////  . . .]
MathStatChem
Level VII

Re: Set submatrix values within a matrix

Thanks.  I've never used Transform Each( ), look pretty useful, and would not of thought using that on a matrix.  

Craige_Hales
Super User

Re: Set submatrix values within a matrix

m = j(7,8,99);
m[2::5,3::4] = [
	41 42,
	43 44,
	45 46,
	47 48
];
show(m)
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];
Craige
MathStatChem
Level VII

Re: Set submatrix values within a matrix

Thanks @Craige_Hales , that's what I was looking for.  To make it more general, here is what I am doing

m=J(7,8,99);

update=[41 42,
	43 44,
	45 46,
	47 48
];

m[1::nrows(update), 1::ncols(update)]=update;
show(m);

result

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];

Recommended Articles