I need to loop through an array using the for clause, but starting at some specific index and just to a maximum of iterations.
The code below does the task, but it looks awful to me: it's there a better way?
var offset = 10, max = 5;
for (var i = 0; (i + offset) < data.length && i < max; i++) {
doSomething(data[i + offset]);
}
If I am understanding your question correctly you would just need to initialize i to the offset.
var offset = 10, max = 5 + offset;
for (var i = offset; i < data.length && i < max; i++) {
doSomething(data[i]);
}
edit: didn't understand the max at first.
Related
For instance for N highest numbers, lets say N = 3
I have a and want to get b
a = np.array([12.3,15.4,1,13.3,16.5])
b = ([15.4,13.3,16.5])
Thanks in advance.
well, my take on this:
Make a copy of the original array;
Sort the copied array to find the n highest numbers;
Go through the original array and after comparing its numbers to n highest numbers from the previous step move needed ones in a resulting array.
var a = [12.3,15.4,1,13.3,16.5], n = 3, x = 0, c =[]; // c - the resulting array
var b = a.slice(); // copy the original array to sort it
for(var i = 1; i < b.length; i++) { // insertion sorting of the copy
var temp = b[i];
for(var j = i - 1; j >= 0 && temp > b[j]; j--) b[j + 1] = b[j];
b[j + 1] = temp;
}
for(var i = 0; i < a.length; i++) { // creating the resulting array
for(var j = 0; j < n; j++) {
if(a[i] === b[j]) {
c[x] = a[i]; x++; // or just c.push(a[i]);
}
}
}
console.log(c);
The example is written in Javascript and is somewhat straightforward, but, in fact, it is quite language agnostic and does the job.
I'm trying to do Sort method, however i get this error:
IndexOutOfRangeException, on the line if(chars[i] > chars1[y]). Amount is equal to 25
string temp1;
for (int i = 0; i < amount; i++)
{
for (int y = i + 1; y < amount - 1; y++)
{
var chars = Duomenys[i].Pozicija.ToCharArray();
var chars1 = Duomenys[y].Pozicija.ToCharArray();
if (chars[i] > chars1[y])
{............}
You are using the same indices (i and y) to identify locations within the array Duomenys and chars/chars1, which seem like very different things. Can't tell what you should be doing instead, given the lack of information provided.
Can anyone give an algorithm to find the number of times a number repeats in pascal's triangle? For example
num - No of times
1 - infinite
2 - 1
3 - 2
4 - 2
. .
6 - 3
. .
10 - 4
. .
for image Link
Or in other way, how many nCr 's are possible for nCr = x , where x is any given integer?
Just count. You know n > 1 can only appear in the first n+1 rows of Pascal's triangle. And that each row is symmetric, and increasing (for the first half). That saves time.
See http://oeis.org/A003016 for more about the sequence
I had to write something similar for a hackathon challenge. This code will find all the numbers 1 to MAX_NUMBER_TO_SEARCH that have a count of more than MINIMUM_COUNT in the Pascal Triangle of size PASC_SIZE. You can obviously alter it to only count for a single number. Not super efficient obviously.
function pasc(n) {
var xx = [];
var d = 0;
var result = [];
result[0] = [1];
result[1] = [1, 1];
for (var row = 2; row < n; row++) {
result[row] = [1];
for (var col = 1; col <= row - 1; col++) {
result[row][col] = result[row - 1][col] + result[row - 1][col - 1];
result[row].push(1);
}
for (var ff = 0; ff < result[row].length; ff++) {
xx[d++] = (result[row][ff]);
}
}
return xx;
}
function countInArray(array, what) {
var count = 0;
for (var i = 0; i < array.length; i++) {
if (array[i] === what) {
count++;
}
}
return count;
}
var MAX_NUMBER_TO_SEARCH = 5000;
var MINIMUM_COUNT = 5;
var PASC_SIZE = 1000;
var dataset = pasc(PASC_SIZE);
for (var i = 0; i < MAX_NUMBER_TO_SEARCH; i++) {
if (countInArray(dataset, i) >= MINIMUM_COUNT) {
console.log(i + " Count:" + countInArray(dataset, i) + "\n");
}
}
I would like to separate objects in OpenCv like the following image it shows:
But if I am using cvDilate or cvErode the objects grow together... how to do that with OpenCv?
It looks like you will need to write your own dilate function and then add xor functionality yourself.
Per the opencv documentation, here is the rule that cvdilate uses:
dst=dilate(src,element): dst(x,y)=max((x',y') in element))src(x+x',y+y')
Here is pseudocode for a starting point (this does not include xor code):
void my_dilate(img) {
for(i = 0; i < img.height; i++) {
for(j = 0; j < img.width; j++) {
max_pixel = get_max_pixel_in_window(img, i, j);
img.pixel(i,j) = max_pixel;
}
}
}
int get_max_pixel_in_window(img, center_row, center_col) {
int window_size = 3;
int cur_max = 0;
for(i = -window_size; i <= window_size; i++) {
for(j = -window_size; j <= window_size; j++) {
int cur_col = center_col + i;
int cur_row = center_row + j;
if(out_of_bounds(img, cur_col, cur_row)) {
continue;
}
int cur_pix = img.pixel(center_row + i, center_col + j);
if(cur_pix > cur_max) {
cur_max = cur_pix;
}
}
}
return cur_max;
}
// returns true if the x, y coordinate is outside of the image
int out_of_bounds(img, x, y) {
if(x >= img.width || x < 0 || y >= img.height || y <= 0) {
return 1;
}
return 0;
}
As far as I know OpenCV does not have "dilation with XOR" (although that would be very nice to have).
To get similar results you might try eroding (as in 'd'), and using the eroded centers as seeds for a Voronoi segmentation which you could then AND with the original image.
after erosion and dilate try thresholding the image to eliminate weak elements. Only strong regions should remain and thus improve the object separation. By the way could you be a little more clear about your problem with cvDilate or cvErode.
Is there a performance hit when iterating over object attributes vs. iterating an array?
Example, using objects:
var x:Object = {one: 1, two: 2, three: 3};
for (var s:String in x) {
trace(x[s]);
}
Vs using an array
var a:Array = [1, 2, 3];
var len:Number = a.length;
for (var i:Number = 0; i < len; ++i) {
trace(a[i]);
}
So - which is faster and most importantly by what factor?
IIRC, in some JavaScript implementation iterating over objects attributes is slower up to 20x but I haven't been able to find such measurement for ActionScript2.
I just tried a very similar test, but iterating just once over 200k elements, with opposite results:
Task build-arr: 2221ms
Task iter-arr: 516ms
Task build-obj: 1410ms
Task iter-obj: 953ms
I suspect Luke's test is dominated by loop overhead, which seems bigger in the array case.
Also, note that the array took significantly longer to populate in the first place, so ymmv if your task is insert-heavy.
Also, in my test, storing arr.length in a local variable gave a measurable performance increase of about 15%.
Update:
By popular demand, I am posting the code I used.
var iter:Number = 200000;
var time:Number = 0;
var obj:Object = {};
var arr:Array = [];
time = getTimer();
for (var i:Number = 0; i < iter; ++i) {
arr[i] = i;
}
trace("Task build-arr: " + (getTimer() - time) + "ms");
time = getTimer();
for (var i:Number = 0; i < iter; ++i) {
arr[i] = arr[i];
}
trace("Task iter-arr: " + (getTimer() - time) + "ms");
time = getTimer();
for (var i:Number = 0; i < iter; ++i) {
obj[String(i)] = i;
}
trace("Task build-obj: " + (getTimer() - time) + "ms");
time = getTimer();
for (var i:String in obj) {
obj[i] = obj[i];
}
trace("Task iter-obj: " + (getTimer() - time) + "ms");
OK. Why not do some simple measurements?
var time : Number;
time = getTimer();
var x:Object = {one: 1, two: 2, three: 3};
for( i = 0; i < 100000; i++ )
{
for (var s:String in x)
{
// lets not trace but do a simple assignment instead.
x[s] = x[s];
}
}
trace( getTimer() - time + "ms");
time = getTimer();
var a:Array = [1, 2, 3];
var len:Number = a.length;
for( i = 0; i < 100000; i++ )
{
for ( var j : Number = 0; j < len; j++)
{
a[j] = a[j];
}
}
trace( getTimer() - time + "ms");
On my machine the array iteration is somewhat slower. This could be because ActionScript 2 doesn't have 'real' arrays but only associative arrays (maps). Apparently to work with an array the compiler has to generate some code overhead. I haven't looked into the specifics of this but I can imagine that that could be the case.
BTW. Doing this test might also show that putting the array length value into a variable doesn't really increase performance either. Just give it go....
UPDATE: Even though ActionScript and JavaScript are syntactically related, the underlying execution mechanism is completely different. E.g. FireFox uses SpiderMonkey and IE will probably use a Microsoft implementation whereas AS2 is executed by the Adobe's AVM1.