Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1100755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:55:49+00:00 2026-05-17T00:55:49+00:00

This is comp sci 101 stuff, but I couldn’t find an answer applicable to

  • 0

This is comp sci 101 stuff, but I couldn’t find an answer applicable to R (or matlab).

I have a for loop that I want to initialize with a first guess (all zeros here, but maybe something else later), but I want to keep updating with each iteration. What I have below works, but it kind of clunky and embarrassing.

I would like to avoid the one iteration before the for loop. I could do it with an ifelse inside the loop, but that seems inefficient. Thanks!

alpha <- 0.3
beta <- 0.6
m <- 5 # elements in k
n <- 10 # iterations
k.prime <- v <- matrix(0, n, m)
k <- seq(from=0.04, to=0.2, length.out=m) # poss values for k
colnames(v) <- colnames(k.prime) <- round(k, digits=2)

# first loop for taking the first guess for v()
i <- 1
for (j in 1:m) {
    temp.v <- log(k[j]^alpha - k) + beta*rep(0, times=m)
    v[i, j] <- max(temp.v)
    k.prime[i, j] <- k[which.max(temp.v)]
}

# remaining loops
for (i in 2:n) {
    for (j in 1:m) {
        temp.v <- log(k[j]^alpha - k) + beta*v[i-1, ]
        v[i, j] <- max(temp.v)
        k.prime[i, j] <- k[which.max(temp.v)]
    }
}

v 
k.prime
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:55:50+00:00Added an answer on May 17, 2026 at 12:55 am

    Init v[1,] with zeroes, delete the first loop and fix i index to i+1 elsewhere.
    This should then look like this:

    alpha<-0.3
    beta<-0.6
    m<-5 #elements in k
    n<-10 #iterations
    k.prime<-matrix(0,n,m);
    v<-matrix(0,n+1,m);
    k<-seq(from=0.04,to=0.2,length.out=m) #poss values for k
    colnames(v)<-colnames(k.prime)<-round(k,digits=2)
    
    
    v[1,]<-rep(0,m);
    
    # remaining loops
    for(i in 1:n){
        for(j in 1:m){
            temp.v<-log(k[j]^alpha-k)+beta*v[i,]
            v[i+1,j]<- max(temp.v)
            k.prime[i,j]<-k[which.max(temp.v)]
        }
    }
    v[-1,]->v; #Cleanup of 0-row
    
    v 
    k.prime
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(I asked a variation of this question on comp.std.c++ but didn't get an answer.)
Sorry if this is a comp-sci 101 question. I'm just unsure if I'm missing
This is part of my first comp sci assignment, we are writing a C
I'm trying to figure out this problem for one of my comp sci classes,
There was a thread on this in comp.lang.javascript recently where victory was announced but
I have String like this: LODIXAL COMP 15 How can i split it to
I have a snippet that is like this: <div id=image_input> <img src=missing-image.png name=image_comp />
I have a simple question for a Comp Sci class I'm taking where my
Forgive me if this is a silly question....but I think back to my Comp.
I have essentially a survey that is shown, and people answer questions a lot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.