Just started using Scilab, looks like a lot to learn but I am stuck at very first basic program. I need to display 1 to 10 numbers without using loop.
I know that using loop we can use this code to display numbers from 1 to 10:
for i = 1:10
disp(i)
end
But i need to display them without using any loop. In C programming it's a bit easy using recursive function but here i have tried alot and failed.
Please can any one help me out via code snippet.
I think a recursive call is still kind of a loop, but it could be done like this:
function recursivePrint(i, maxNumber)
if( i <= maxNumber )
disp(i)
recursivePrint(i+1, maxNumber)
end
endfunction
recursivePrint(1, 10);
Usually when people ask to write a function without a loop they mean something like:
disp(1:10)
disp(1)
disp(2)
disp(3)
disp(4)
disp(5)
disp(6)
disp(7)
disp(8)
disp(9)
disp(10)
Curious what you tried..
Related
I have created a "while" loop that looks like this:
x <- 2
while (x < 8) {
print("Shrek")
}
Now, I have tried breaking the loop by inserting break in to the while loop, like this:
while (x < 8) {
print("Shrek")
break
}
but this doesn't break the loop, the console just keeps printing "Shrek".
How can i make it stop? Have I put break in the wrong place?
Side question: Can there arise any problems from exiting Rstudio while it's in an on-going while() loop like this?
Thanks!
Edit:
Pressing escape stopped the loop. Is there any line of code in the console that also stops it?
Based on the code you have given I can't reproduce this. When I run everything, break works just fine. Have you potentially defined a variable called 'break' that would have overridden the default function? If not try resetting R and running this again.
I have a command with six lines that I want to use several times. Therfore, I want to assign a name to this command and use it as a procedure instead of writing the whole command lines over and over.
In this case it is a <-rbind() command, but the issue is also more general.
modelcoeff<-rbind(modelcoeff,c(as.character((summary(mymodel)$terms[[2]])[[3]]),
as.character((((((summary(mymodel)$terms[[2]])[[2]])[[3]])[[3]])[[2]])[[3]]),
summary(mymodel)$coefficients[2,1],
summary(mymodel)$coefficients[2,4],
summary(mymodel)$coefficients[2,2],
summary(mymodel)$r.squared*100))
I would like to call something like rbindmodelcoeff and execute these command lines. How can I achieve this?
I tried to write a function, but it didn't seem to be the right approach.
A literal wrapping of your code into a function:
rbindmodelcoeff <- function(modelcoeff, mymodel) {
rbind(modelcoeff,
c(as.character((summary(mymodel)$terms[[2]])[[3]]),
as.character((((((summary(mymodel)$terms[[2]])[[2]])[[3]])[[3]])[[2]])[[3]]),
summary(mymodel)$coefficients[2,1],
summary(mymodel)$coefficients[2,4],
summary(mymodel)$coefficients[2,2],
summary(mymodel)$r.squared*100))
}
However, there are a couple changes I recommend:
call summary(mymodel) once, then re-use the results
you are using as.character on some of the objects but not all within the enclosing c(.), so everything is being converted to a character; to see what I mean, try c(as.character(1), 2); we can use a list instead to preserve string-vs-number
rbindmodelcoeff <- function(modelcoeff, mymodel) {
summ <- summary(mymodel)
rbind(modelcoeff,
list(as.character((summ$terms[[2]])[[3]]),
as.character((((((summ$terms[[2]])[[2]])[[3]])[[3]])[[2]])[[3]]),
summ$coefficients[2,1],
summ$coefficients[2,4],
summ$coefficients[2,2],
summ$r.squared*100))
}
But there are still some problems with this. I can't get it to work at the moment since I don't know the model parameters you're using, so as.character((summ$terms[[2]])[[3]]) for me will fail. With that, I'm always hesitant to hard-code so many brackets without a firm understanding of what is being used. It's out of scope for this question (which is being converting your basic code into a function), but you might want to find out how to generalize that portion a bit.
I want to work out a simple recurrence relation in R but I cant find resources anywhere online that allows me to do this.
something simple like
for $n=1,2,3$
$X_0=3$
$X_n=X_{n-1}+5$
then print all $X_n$
I understand the basics for a for loop but it doesnt work for the left hand side of the loop, like i cant simply have
$for(i \hspace{2mm} in \hspace{2mm} 1:3)$
{
$X_0=3$
$X_n=X_{n-1}+5$
}
Like with any other language, you could use a for loop but then you're not using recursivity. The recursive version:
f <- function(n) { if (n==0) 3 else f(n-1)+5 }
I have a data.table like this:
testDT <- data.table(SFf = as.character(c("C1H1", "C3H4Cl")),
Mult = as.integer(c(3,5))
)
now I am trying to loop over the rows of this table and apply a function (multiform() from the package enviPat) to it like this:
mapply(multiform,testDT$SFf,testDT$Mult)
the output of it should be two strings, namely "C3H3" and "C15H20Cl5". However the function simply never stops calculating and seems to get caught up in an endless loop.
When I apply the function like
multiform("C1H1",3)
it works and I get "C3H3" as output. What am I doing wrong?
Yasel
I got it. The problem was that the function multiform() does not allow elements without numbers behind them.
So it wont take "C3H4Cl" but "C3H4Cl1".
I don't think that this is intended and will try to contact the authors of the package.
lets say I have written a function for adding two numbers.
function [result] = add_twonum(a1,a2)
result = a1+a2;
endfunction
in the main file, when I call
result = add_twonum(1,2)
I expect result = 3 .
However it says
!--error 4
Undefined variable: add_twonum
Please help me. I have used Matlab a lot but never scilab. I tried every possible way I know of. like changing current directory etc etc.
In the main file, you need to execute the function first, with
exec('add_twonum.sci');
assuming add_twonum.sci is the file which contains your function. Then you can call the function:
result = add_twonum(a1,a2);