cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
%3CLINGO-SUB%20id%3D%22lingo-sub-792046%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EAre%20there%20ways%20to%20populate%20a%205x5%20matrix%20with%20values%20101..505%20without%20using%20loops%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-792046%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3ESurely%20there%20are%20ways!%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELoop%20version%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Eii%20%3D%201%3A%3A5%3B%20jj%20%3D%201%3A%3A5%3B%0A%0Am%20%3D%20j(5%2C5)%3B%0A%0Aforeach(%7Bi%7D%2C%20ii%2C%0A%09foreach(%7Bj%7D%2C%20jj%2C%0A%09%09m%5Bi%2Cj%5D%20%3D%20i*100%20%2B%20j%0A%09)%0A)%3B%0A%0Am%3B%0A%0A%23%3D%26gt%3B%20%5B%20101%20102%20103%20104%20105%2C%20%0A%09201%20202%20203%20204%20205%2C%20%0A%09301%20302%20303%20304%20305%2C%20%0A%09401%20402%20403%20404%20405%2C%20%0A%09501%20502%20503%20504%20505%5D%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EMultiple%20solutions%20encouraged%2C%20if%20you%20have%20them!%20I'm%20trying%20to%20%22get%20fluent%22%20in%20matrices.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-792046%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CLINGO-LABEL%3EAutomation%20and%20Scripting%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-792136%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20Are%20there%20ways%20to%20populate%20a%205x5%20matrix%20with%20values%20101..505%20without%20using%20loops%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-792136%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EGot%20one%2C%20finally.%20It%20ain't%20pretty%20though%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Ea%20%3D%20shape(mod(0%3A%3A24%2C%205)%20%2B%201%2C%205)%3B%0A%0A%23%3D%26gt%3B%20%5B1%202%203%204%205%2C%0A1%202%203%204%205%2C%0A1%202%203%204%205%2C%0A1%202%203%204%205%2C%0A1%202%203%204%205%5D%0A%0Ab%20%3D%20shape(floor(index(10%2C%2058%2C%202)%20%2F%2010)%2C%205)%20*%20100%3B%0A%0A%23%3D%26gt%3B%20%5B100%20100%20100%20100%20100%2C%0A200%20200%20200%20200%20200%2C%0A300%20300%20300%20300%20300%2C%0A400%20400%20400%20400%20400%2C%0A500%20500%20500%20500%20500%5D%0A%0Aa%2Bb%3B%0A%0A%23%3D%26gt%3B%20%5B101%20102%20103%20104%20105%2C%0A201%20202%20203%20204%20205%2C%0A301%20302%20303%20304%20305%2C%0A401%20402%20403%20404%20405%2C%0A501%20502%20503%20504%20505%5D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-792155%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20Are%20there%20ways%20to%20populate%20a%205x5%20matrix%20with%20values%20101..505%20without%20using%20loops%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-792155%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Ex%20%3D%20J(%205%2C%205%2C%20.%20)%3B%0AParallel%20Assign(%20%7B%7D%2C%20x%5Br%2C%20c%5D%20%3D%20100%20*%20r%20%2B%20c%20)%3B%0Ax%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CPRE%3E%5B101%20102%20103%20104%20105%2C%0A201%20202%20203%20204%20205%2C%0A301%20302%20303%20304%20305%2C%0A401%20402%20403%20404%20405%2C%0A501%20502%20503%20504%20505%5D%3C%2FPRE%3E%0A%3CP%3Eor%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3En%20%3D%205%3B%0Ax%20%3D%201%20%3A%3A%20n%3B%0Ay%20%3D%20100%20*%20x%3B%0ARepeat(%20x%2C%20n%20)%20%2B%20Repeat(%20y%2C%20n%20)%60%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CPRE%3E%5B101%20102%20103%20104%20105%2C%0A201%20202%20203%20204%20205%2C%0A301%20302%20303%20304%20305%2C%0A401%20402%20403%20404%20405%2C%0A501%20502%20503%20504%20505%5D%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-792391%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20Are%20there%20ways%20to%20populate%20a%205x5%20matrix%20with%20values%20101..505%20without%20using%20loops%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-792391%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3E(101%3A%3A105)%20%7C%2F%20(201%3A%3A205)%20%7C%2F%20(301%3A%3A305)%20%7C%2F%20(401%3A%3A405)%20%7C%2F%20(501%3A%3A505)%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-792412%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20Are%20there%20ways%20to%20populate%20a%205x5%20matrix%20with%20values%20101..505%20without%20using%20loops%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-792412%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3ECan%20get%20slow%20but%20you%20can%20also%20use%20JSL%20code%20with%20J()%20function%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ENames%20Default%20To%20Here(1)%3B%0A%0An%20%3D%205%3B%0A%0Ai%20%3D%201%3B%0Am%20%3D%20J(n%2C%20n%2C%20%0A%09Ceiling(i%20%2F%20n)%20*%20100%20%2B%20Mod(i%2B%2B%20-%201%2C%20n)%20%2B%201%0A)%3B%3CBR%20%2F%3E%3CBR%20%2F%3EWrite()%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
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)