I am using Visual Studio 2012 for creating a web application server with vb.net and asp.net. I once had 116 separate arrays all with 116 values. I'm realizing now that it would be easier to run the calculations that I want if I used one 2D array instead. My 2D array has 116 sections of 116 integers. As soon as I complete the array with "}", Visual Studio crashes and restarts.
Is there a size limit to 2D arrays? Is there a step that I'm missing? Thanks!
My code looks like:
Dim data(,) As Integer = {{0, 9, 5, 7, 5, 7, 6, 6, 7, 2, 4, 2, 5, 7, 4, 6, 5, 3, 5...etc}, _
{9, 0, 7, 6, 6, 2, 5, 8, 8, 1, 5, 1, 7, 7, 6, 6, 7, 3, 7...etc}, _
{5, 7, 0, 7, 6, 5, 4, 4, 5, 2, 4, 3, 8, 5, 7, 8, 3, 5, 4...etc}, _
................[112 more of this].......................
{5, 8, 5, 8, 7, 7, 9, 0, 7, 2, 4, 2, 5, 7, 4, 6, 5, 3, 7...etc}}
^
'This is where crash happens
EDIT: I've done some testing and it seems to break around 40 lines.
Sounds like you should consider a matrix, or use a list(0f)
Creating 2-dimensional array with unlimited size?
Also, a possibility is a dictionary, though I'm not sure what you are doing so its hard to say. Maybe, tell us what you are using the data for?
Nice bit of explaination regarding generics, lists, collections, and arrays. As noted in the comment above, I think you should dimension your array properly array(115,115) as the case may be. Or ReDim Preseve as you discover the size
http://www.dreamincode.net/forums/topic/333038-arrays-and-collections-overview/
Related
I am learning to use various forecasting packages available in R, and came across bsts(). The data I deal with is a time series of demands.
data=c(27, 2, 7, 7, 9, 4, 3, 3, 3, 9, 6, 2, 6, 2, 3, 8, 6, 1, 3, 8, 4, 5, 8, 5, 4, 4, 6, 1, 6, 5, 1, 3, 0, 2, 6, 7, 1, 2, 6, 2, 8, 6, 1, 1, 3, 2, 1, 3, 1, 6, 3, 4, 3, 7, 3, 4, 1, 7, 5, 6, 3, 4, 3, 9, 2, 1, 7, 2, 2, 9, 4, 5, 3, 4, 2, 4, 4, 8, 6, 3, 9, 2, 9, 4, 1, 3, 8, 1, 7, 7, 6, 0, 1, 4, 8, 9, 2, 5)
ts.main=ts(data, start=c(1910,1), frequency=12)
ss <- AddLocalLinearTrend(list(), y=ts.main)
ss <- AddSeasonal(ss, y=as.numeric(ts.temp), nseasons=12)
model <- bsts(as.numeric(ts.temp),
state.specification = ss,
niter = 1000)
pred <- predict(model, horizon = 12)
Is there way I can restrict pred$mean from becoming negative?
Since your data are a time series of counts, you need to take that into account rather than assume Gaussian errors; for some discussion on this and elaboration of some approaches, see for example Brandt et al 2000 and Brandt and Williams 2001. Luckily, the bsts package has a built-in functionality for this, the family option (see pages 24 to 26 of the documentation).
So, you can just do this
model <- bsts(as.numeric(ts.main),
state.specification = ss,
family = 'poisson',
niter = 1000)
so that the bsts() function correctly considers the data as counts, which will solve your issue, since the draws from the posterior predictive distribution will then be non-negative by definition.
How do I find the intersection of two lists including duplicates in mathematica?
So, If I have this:
list1 = {1, 1, 3, 4, 5, 6, 6, 6, 7, 7, 10, 11, 11};
list2 = {1, 1, 4, 5, 5, 6, 6, 7, 7, 8, 11, 11, 13, 14};
I'd want it to return this:
IntersectionIncludingDuplicates[list1, list2] = {1, 1, 4, 5, 6, 6, 7, 7, 11, 11}
Thanks for any and all help!
Here's one way:
Catenate#KeyValueMap[ConstantArray]#
MapThread[Min, KeyIntersection[Counts /# {list1, list2}]]
Breaking it down:
Count how many times each element occurs in each list (Counts)
Retain only those elements which occur in both (KeyIntersection)
Take the smaller number of occurrences (MapThread, Min) and replicate the given element that many times (ConstantArray)
Edit : fixed and tested..
you might use regular Intersection , then Count , something like
ConstantArray[#, Min[Count[list1, #], Count[list2, #]]] & /#
Intersection[list1, list2] // Flatten
{1, 1, 4, 5, 6, 6, 7, 7, 11, 11}
in functional form generalised to take an arbitrary number of lists:
IntersectionIncludingDuplicates[lists__List] :=
ConstantArray[#,
Function[{v}, Min ## (Count[#, v] & /# {lists})]##] & /#
Intersection[lists] // Flatten
IntersectionIncludingDuplicates[list1, list2]
{1, 1, 4, 5, 6, 6, 7, 7, 11, 11}
Simple code to understand. Assumes input lists are sorted.
list1 = {1, 1, 3, 4, 5, 6, 6, 6, 7, 7, 10, 11, 11};
list2 = {1, 1, 4, 5, 5, 6, 6, 7, 7, 8, 11, 11, 13, 14};
IntersectionIncludingDuplicates[list1_, list2_] := Module[
{out = {}, i = j = 1},
While[i <= Length[list1] && j <= Length[list2],
If[list1[[i]] == list2[[j]],
AppendTo[out, list1[[i]]];
i++; j++,
If[list1[[i]] < list2[[j]], i++, j++]]];
out]
IntersectionIncludingDuplicates[list1, list2]
{1, 1, 4, 5, 6, 6, 7, 7, 11, 11}
I'm have to use R instead of Matlab and I'm new to it.
I have a large array of data repeating like 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10...
I need to find the locations where values equal to 1, 4, 7, 10 are found to create a sample using those locations.
In this case it will be position(=corresponding value) 1(=1) 4(=4) 7(=7) 10(=10) 11(=1) 14(=4) 17(=7) 20(=10) and so on.
in MatLab it would be y=find(ismember(x,[1, 4, 7, 10 ])),
Please, help! Thanks, Pavel
something like this?
foo <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
bar <- c(1, 4, 7, 10)
which(foo %in% bar)
#> [1] 1 4 7 10 11 14 17 20
#nicola, feel free to copy my answer and get the recognition for your answer, simply trying to close answered questions.
The %in% operator is what you want. For example,
# data in x
targets <- c(1, 4, 7, 10)
locations <- x %in% targets
# locations is a logical vector you can then use:
y <- x[locations]
There'll be an extra step or two if you wanted the row and column indices of the locations, but it's not clear if you do. (Note, the logicals will be in column order).
I have a problem about algorithm.
Given 2d matrix such:
2, 1, 2, 5, 5, 0
1, 4, 0, 1, 0, 8
2, 8, 4, 1, 7, 1
5, 6, 4, 9, 7, 9
8, 7, 9, 6, 2, 5
6, 6, 7, 4, 8, 3
Question: use "up", "left", "right", "down" moving to find path has length is 10 (can't revisit a node).
Example:
2, 1, 2, 5, 5, 0
1, 4, 0, [1], 0, 8
2, 8, 4, [1], [7], [1]
5, 6, 4, 9, 7, 9
8, 7, 9, 6, 2, 5
6, 6, 7, 4, 8, 3
More specifically, the algorithm needs to answer the question: exist or not exist such a way
I've solved the problem by dividing into 2 small problems:
subset sum problem
check path exists through a set of nodes
What I need to do is test the prior n number of rows (say 5 for a nice round number) of a data.frame. This is really easy for just one, where it's
for (i in 1:nrow(data)){
ifelse(((data$V1[i]>(mean(data$V1)+2*sd(data$V1))) &
(data$V1[i-1]>(mean(data$V1)+2*sd(data$V1)))),Control[i,1]<-1,Control[i,1]<-0)
}
This works and Control is filled with 1s if the test is true and 0 if it is false.
However, I want to extend it to several more in the past, which I attempt to do with a nested for like so:
for (i in 1:nrow(data$V1)){ifelse((data$V1[i]>(mean(data$V1)+sd(data$V1))) &
(for (j in 1:4){(data$V1[(i-j)]>(mean(data$V1)+sd(data$V1)))},
Control[i,1]<-1,Control[i,1]<-0)}
This gives the following error (for simplicity I test with a single vector of values, called test ):
Error: unexpected ',' in "for (i in 1:length(test)){ifelse((test[i]>(mean(test)+sd(test))) & (for (j in 1:4){(test[(i-j)]>(mean(test)+sd(test)))},"
Trying to pad it with some parens gives the following slightly different error:
Error: unexpected ')' in "for (i in 1:length(test)){ifelse((test[i]>(mean(test)+sd(test))) & (for (j in 1:4){(test[(i-j)]>(mean(test)+sd(test))))"
test is defined like so:
test <- c(4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 8, 8, 7, 7)
Any help with my method, or a more efficient R method (I'm still new to the language!) is extremely welcome.
I think you need rollapply from zoo package. But it is not clear how you define your loop since you use data? Control and yo give an error in a test object.... Here I check if the current value and 4 previous values are all less than a certain value (outlier). No need to use ifelse here since the condition is not a vector.
test <- c(4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 8, 8, 7, 7)
library(zoo)
V <- mean(test)+2*sd(test)
rollapply(test,5,function(x)if(all(x>V)) 1 else 0)