for-loop

How to store value generated from nested for loop in an array, in Matlab?

喜你入骨 提交于 2020-01-11 13:16:15
问题 y = find(sA); l = y + sA; for i=1:10 for j=1 l = l + sA; end y = y + length(y); end I would like to know how to store the value that is generated for l , for each iteration, in an array. When I try do something like l(l) = l + sA; I obtain 'weird' results. NOTE: PLEASE READ MY COMMENTS POSTED BELOW. THANKS! 回答1: For a complex loop, usually I do something like this: results = zeros(expectedLength,1); ixNextResult = 1; for ixForLoop1 = 1:10 for ixForLoop2 = 20:30 .. results(ixNextResult) =

for loop problem [duplicate]

99封情书 提交于 2020-01-11 13:14:08
问题 This question already has an answer here : Script skips second for loop when reading a file (1 answer) Closed 4 years ago . For loop issue: in1 = open('file_1', 'r') in2 = open('file_2', 'r') outf = open('out_file', 'w') for line in in1: s = line.split('\t') A = s[1][:-1] B = s[0] counter = 0 for line in in2: ss = line.split('\t') if A == ss[0] or A == ss[1]: counter += 1 outf.write('%s\t%s\t%s\n'%(A,B,counter)) The problem is that it is only going through for line in in2: for the first line

Get value from other column based on value of a column

浪尽此生 提交于 2020-01-11 13:12:04
问题 For each row in a data frame I want to copy a value from one column to another depending on a value in third column. I tried to do it with a combined for loop and if function: ## example condition <- c("1","2","2","1","2","","3","3") SZ01 <- c("1","1","1","1","1","","1","1") SZ02 <- c("2","2","2","2","2","","2","2") SZ03 <- c("3","3","3","3","3","","3","3") df <- data.frame(cbind(condition,SZ01,SZ02,SZ03), stringsAsFactors = FALSE) df$retribution <- NULL df$special_prevention <- NULL df

For loop in R assigning to a data frame

隐身守侯 提交于 2020-01-11 13:11:43
问题 I am having trouble assigning to a dataframe after running the for loop. When I use print it give my value, any explanation to it? salesdate<-rep(seq(from=as.Date("2013-12-19"),to=as.Date("2013-12-23"),by=1),100) memberid<-as.factor(sample(1:1000,500)) msales<-data.frame(salesdate,memberid) new<-seq(from=as.Date("2013-12-19"),to=as.Date("2013-12-23"),by=1) for(i in new) + print(length(unique(msales[(msales$salesdate>="2013-12-23" | msales$salesdate>=i),]$memberid))) [1] 500 [1] 400 [1] 300 [1

reversing only certain words of a string

笑着哭i 提交于 2020-01-11 13:11:09
问题 I have a string with the name "Mustang Sally Bob" After i run my code i want the string output to be like this: gnatsuM yllaS boB My approach is to count the words until the space and save the index of where the space is located in the string. then Then I want to print the characters starting from the space backwards. #include <stdio.h> int main() { char* test="Mustang Sally Bob"; int length; //string length int x; for(length=0;test[length] !=0&&test[length];length++); //get string length int

For loop in R assigning to a data frame

我只是一个虾纸丫 提交于 2020-01-11 13:10:14
问题 I am having trouble assigning to a dataframe after running the for loop. When I use print it give my value, any explanation to it? salesdate<-rep(seq(from=as.Date("2013-12-19"),to=as.Date("2013-12-23"),by=1),100) memberid<-as.factor(sample(1:1000,500)) msales<-data.frame(salesdate,memberid) new<-seq(from=as.Date("2013-12-19"),to=as.Date("2013-12-23"),by=1) for(i in new) + print(length(unique(msales[(msales$salesdate>="2013-12-23" | msales$salesdate>=i),]$memberid))) [1] 500 [1] 400 [1] 300 [1

Loop through object variables with different number on the name

为君一笑 提交于 2020-01-11 10:18:51
问题 I have the following class: public class Employees { public string field1 { get; set; } public string field2 { get; set; } public string field3 { get; set; } public string field4 { get; set; } } And i want to change values to all those members. so i can to something like that: Employees.field1 = "ghgf"; Employees.field2 = "ghgf"; Employees.field3 = "ghgf"; Employees.field4 = "ghgf"; but it's very ugly. and the amount of members will be 30, so this is not a good way... I want to use for loop,

For loop in the form of : “for (A b : c)” in Java

我怕爱的太早我们不能终老 提交于 2020-01-11 09:46:10
问题 This is the first time that I've seen this kind of syntax : // class Node public class Node { ... ... } public class Otherclass { ... } Otherclass graph = new Otherclass(); // getSuccessors is a method of Otherclass class Node currentNode ; List<Node> successors = graph.getSuccessors(currentNode); // weird for loop for (Node son : successors) { // do something } What is that for loop ? some kind of a Matlab syntax ? Is there any other way to write that for loop ? Regards 回答1: It's a for each

For-Loop and LINQ's deferred execution don't play well together

故事扮演 提交于 2020-01-11 08:56:30
问题 The title suggests that i've already an idea what's going on, but i cannot explain it. I've tried to order a List<string[]> dynamically by each "column", beginning with the first and ending with the minimum Length of all arrays. So in this sample it is 2 , because the last string[] has only two elements: List<string[]> someValues = new List<string[]>(); someValues.Add(new[] { "c", "3", "b" }); someValues.Add(new[] { "a", "1", "d" }); someValues.Add(new[] { "d", "4", "a" }); someValues.Add(new

For loops (novice)

萝らか妹 提交于 2020-01-11 08:29:11
问题 I recently started learning Python, and the concept of for loops is still a little confusing for me. I understand that it generally follows the format for x in y , where y is just some list. The for-each loop for (int n: someArray) becomes for n in someArray , And the for loop for (i = 0; i < 9; i-=2) can be represented by for i in range(0, 9, -2) Suppose instead of a constant increment, I wanted i*=2 , or even i*=i . Is this possible, or would I have to use a while loop instead? 回答1: As you