Hi, I have another coding issue. I would like to fill a matrix using a for loop so that each cell in a column is equal to the value of the cell above it multiplied by (1 - the value of the cell above the corresponding cell in another matrix). Therefore, using microsoft excel to demonstrate, B2 = B1*(1-A1) and then dragging down. I've been using @subextract to extract just the one cell so it looks like this:
For !m=1 to @rows(x)
If !m>=2 then
x(!m)=(@subextract(x,!m-1,1,!m-1,1))*(@subextract(units,!m,1,!m,1)-(@subextract(y,!m-1,1,!m-1,1)))
endif
next
; where x is currently a vector and y is the other matrix (with the info) and units is a "ones" matrix. I figured I would nest this loop within another loop (if x was a nxn matrix instead) so that I can do the same procedure for every column. When I run the above code it tells me non-numeric argument in "x(2)=@subextract...."
Thanks heaps,
kuuks.
loops to fill a matrix
Moderators: EViews Gareth, EViews Moderator, EViews Jason, EViews Matt
-
EViews Gareth
- Fe ddaethom, fe welon, fe amcangyfrifon
- Posts: 13598
- Joined: Tue Sep 16, 2008 5:38 pm
Re: loops to fill a matrix
Code: Select all
for !i=2 to @rows(x)
x(!i, 1) = x(!i-1, 1)*(1-y(!i,1))
next
Re: loops to fill a matrix
Thanks Gareth that works very well. I just had to fill x with ones first, otherwise the vector fills with zeros. Regarding the greater loop, i turned x into a matrix and did as you said and every other column just filled with ones. The code is:
matrix x=@ones(101, 77)
For !n=1 to @columns(x)
For !m=2 to @rows(x)
x(!m,1)=x(!m-1,1)*(1-y(!m-1,1))
next
next
What am I missing?
matrix x=@ones(101, 77)
For !n=1 to @columns(x)
For !m=2 to @rows(x)
x(!m,1)=x(!m-1,1)*(1-y(!m-1,1))
next
next
What am I missing?
Re: loops to fill a matrix
Ah I got it sorry. I forgot to put !n instead of 1 for the columns in the second loop:
matrix x=@ones(101, 77)
For !n=1 to @columns(x)
For !m=2 to @rows(x)
x(!m,!n)=x(!m-1,!n)*(1-deathprob_1yr(!m-1,!n))
next
next
Thanks again Gareth
matrix x=@ones(101, 77)
For !n=1 to @columns(x)
For !m=2 to @rows(x)
x(!m,!n)=x(!m-1,!n)*(1-deathprob_1yr(!m-1,!n))
next
next
Thanks again Gareth
Who is online
Users browsing this forum: No registered users and 2 guests
