Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Mata looping question

From   Anna Reimondos <>
Subject   st: Mata looping question
Date   Tue, 18 Oct 2011 20:37:50 +1100

I am just starting to try and learn how to use mata and have a
question about a simple loop command.

For example if I have one original matrix called 'a'

matrix a=(1,0,0\0,2,1\0,1,0\0,0,0)
matrix list a

I then want to create another matrix 'b' which is equal to 'a' but
where every element has been shifted up one row and across one column.

matrix b = J(4,3,0)

forvalues i = 1/4 {
local x=`i'+1
	forvalues j = 1/3 {
	local y=`j'+1
		 matrix b[`i',`j']= a[`x',`y']
matrix list b

I have tried reading a lot of tutorials about using mata but am still
confused about how I would go about doing this in mata

Any advice that would help me along my way would be much appreciated,
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index