cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
%3CLINGO-SUB%20id%3D%22lingo-sub-792046%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%E6%9C%89%E6%B2%92%E6%9C%89%E8%BE%A6%E6%B3%95%E5%9C%A8%E4%B8%8D%E4%BD%BF%E7%94%A8%E8%BF%B4%E5%9C%88%E7%9A%84%E6%83%85%E6%B3%81%E4%B8%8B%E7%94%A8%E5%80%BC%20101..505%20%E5%A1%AB%E6%BB%BF%205x5%20%E7%9F%A9%E9%99%A3%EF%BC%9F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-792046%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3E%E8%BE%A6%E6%B3%95%E8%82%AF%E5%AE%9A%E6%9C%89%EF%BC%81%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3E%E5%BE%AA%E7%92%B0%E7%89%88%E6%9C%AC%EF%BC%9A%3C%2FP%3E%3CP%3E%20%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%3E%E9%BC%93%E5%8B%B5%E5%A4%9A%E7%A8%AE%E8%A7%A3%E6%B1%BA%E6%96%B9%E6%A1%88%EF%BC%88%E5%A6%82%E6%9E%9C%E6%9C%89%E7%9A%84%E8%A9%B1%EF%BC%89%EF%BC%81%E6%88%91%E6%AD%A3%E5%9C%A8%E5%8A%AA%E5%8A%9B%E2%80%9C%E7%B2%BE%E9%80%9A%E2%80%9D%E7%9F%A9%E9%99%A3%E3%80%82%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3E%20%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%3E%E8%87%AA%E5%8B%95%E5%8C%96%E5%92%8C%E8%85%B3%E6%9C%AC%E7%B7%A8%E5%AF%AB%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%3E%E5%9B%9E%E8%A6%86%EF%BC%9A%E6%9C%89%E6%B2%92%E6%9C%89%E8%BE%A6%E6%B3%95%E5%9C%A8%E4%B8%8D%E4%BD%BF%E7%94%A8%E5%BE%AA%E7%92%B0%E7%9A%84%E6%83%85%E6%B3%81%E4%B8%8B%E7%94%A8%E5%80%BC%20101..505%20%E5%A1%AB%E6%BB%BF%205x5%20%E7%9F%A9%E9%99%A3%EF%BC%9F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-792136%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3E%E7%B5%82%E6%96%BC%E6%9C%89%E4%B8%80%E5%80%8B%E4%BA%86%E9%9B%96%E7%84%B6%E5%AE%83%E4%B8%A6%E4%B8%8D%E6%BC%82%E4%BA%AE%3C%2FP%3E%3CP%3E%20%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%20%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-792155%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%E5%9B%9E%E8%A6%86%EF%BC%9A%E6%9C%89%E6%B2%92%E6%9C%89%E8%BE%A6%E6%B3%95%E5%9C%A8%E4%B8%8D%E4%BD%BF%E7%94%A8%E5%BE%AA%E7%92%B0%E7%9A%84%E6%83%85%E6%B3%81%E4%B8%8B%E7%94%A8%E5%80%BC%20101..505%20%E5%A1%AB%E6%BB%BF%205x5%20%E7%9F%A9%E9%99%A3%EF%BC%9F%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%3E%E6%88%96%E8%80%85%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%20%3C%2FP%3E%0A%3CP%3E%20%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-792391%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%E5%9B%9E%E8%A6%86%EF%BC%9A%E6%9C%89%E6%B2%92%E6%9C%89%E8%BE%A6%E6%B3%95%E5%9C%A8%E4%B8%8D%E4%BD%BF%E7%94%A8%E5%BE%AA%E7%92%B0%E7%9A%84%E6%83%85%E6%B3%81%E4%B8%8B%E7%94%A8%E5%80%BC%20101..505%20%E5%A1%AB%E6%BB%BF%205x5%20%E7%9F%A9%E9%99%A3%EF%BC%9F%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%3E%E5%9B%9E%E8%A6%86%EF%BC%9A%E6%9C%89%E6%B2%92%E6%9C%89%E8%BE%A6%E6%B3%95%E5%9C%A8%E4%B8%8D%E4%BD%BF%E7%94%A8%E5%BE%AA%E7%92%B0%E7%9A%84%E6%83%85%E6%B3%81%E4%B8%8B%E7%94%A8%E5%80%BC%20101..505%20%E5%A1%AB%E6%BB%BF%205x5%20%E7%9F%A9%E9%99%A3%EF%BC%9F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-792412%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3E%E5%8F%AF%E8%83%BD%E6%9C%83%E8%AE%8A%E6%85%A2%EF%BC%8C%E4%BD%86%E6%82%A8%E4%B9%9F%E5%8F%AF%E4%BB%A5%E5%B0%87%20JSL%20%E7%A8%8B%E5%BC%8F%E7%A2%BC%E8%88%87%20J()%20%E5%87%BD%E6%95%B8%E4%B8%80%E8%B5%B7%E4%BD%BF%E7%94%A8%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%20%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)