cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
New to using JMP? Hit the ground running with the Early User Edition of Discovery Summit. Register now, free of charge.
Register for our Discovery Summit 2024 conference, Oct. 21-24, where you’ll learn, connect, and be inspired.
Choose Language Hide Translation Bar
mtowle419
Level II

Are there ways to populate a 5x5 matrix with values 101..505 without using loops?

Surely there are ways! 

 

Loop version:

 

ii = 1::5; jj = 1::5;

m = j(5,5);

foreach({i}, ii,
	foreach({j}, jj,
		m[i,j] = i*100 + j
	)
);

m;

#=> [ 101 102 103 104 105, 
	201 202 203 204 205, 
	301 302 303 304 305, 
	401 402 403 404 405, 
	501 502 503 504 505];

Multiple solutions encouraged, if you have them! I'm trying to "get fluent" in matrices.

 

 

3 ACCEPTED SOLUTIONS

Accepted Solutions
Craige_Hales
Super User

Re: Are there ways to populate a 5x5 matrix with values 101..505 without using loops?

x = J( 5, 5, . );
Parallel Assign( {}, x[r, c] = 100 * r + c );
x;
[101 102 103 104 105,
201 202 203 204 205,
301 302 303 304 305,
401 402 403 404 405,
501 502 503 504 505]

or

n = 5;
x = 1 :: n;
y = 100 * x;
Repeat( x, n ) + Repeat( y, n )`;
[101 102 103 104 105,
201 202 203 204 205,
301 302 303 304 305,
401 402 403 404 405,
501 502 503 504 505]

 

 

Craige

View solution in original post

Re: Are there ways to populate a 5x5 matrix with values 101..505 without using loops?

(101::105) |/ (201::205) |/ (301::305) |/ (401::405) |/ (501::505)

View solution in original post

jthi
Super User

Re: Are there ways to populate a 5x5 matrix with values 101..505 without using loops?

Can get slow but you can also use JSL code with J() function

Names Default To Here(1);

n = 5;

i = 1;
m = J(n, n, 
	Ceiling(i / n) * 100 + Mod(i++ - 1, n) + 1
);

Write();

 

-Jarmo

View solution in original post

4 REPLIES 4
mtowle419
Level II

Re: Are there ways to populate a 5x5 matrix with values 101..505 without using loops?

Got one, finally. It ain't pretty though

 

a = shape(mod(0::24, 5) + 1, 5);

#=> [1 2 3 4 5,
1 2 3 4 5,
1 2 3 4 5,
1 2 3 4 5,
1 2 3 4 5]

b = shape(floor(index(10, 58, 2) / 10), 5) * 100;

#=> [100 100 100 100 100,
200 200 200 200 200,
300 300 300 300 300,
400 400 400 400 400,
500 500 500 500 500]

a+b;

#=> [101 102 103 104 105,
201 202 203 204 205,
301 302 303 304 305,
401 402 403 404 405,
501 502 503 504 505]

 

jthi
Super User

Re: Are there ways to populate a 5x5 matrix with values 101..505 without using loops?

Can get slow but you can also use JSL code with J() function

Names Default To Here(1);

n = 5;

i = 1;
m = J(n, n, 
	Ceiling(i / n) * 100 + Mod(i++ - 1, n) + 1
);

Write();

 

-Jarmo
Craige_Hales
Super User

Re: Are there ways to populate a 5x5 matrix with values 101..505 without using loops?

x = J( 5, 5, . );
Parallel Assign( {}, x[r, c] = 100 * r + c );
x;
[101 102 103 104 105,
201 202 203 204 205,
301 302 303 304 305,
401 402 403 404 405,
501 502 503 504 505]

or

n = 5;
x = 1 :: n;
y = 100 * x;
Repeat( x, n ) + Repeat( y, n )`;
[101 102 103 104 105,
201 202 203 204 205,
301 302 303 304 305,
401 402 403 404 405,
501 502 503 504 505]

 

 

Craige

Re: Are there ways to populate a 5x5 matrix with values 101..505 without using loops?

(101::105) |/ (201::205) |/ (301::305) |/ (401::405) |/ (501::505)