Coffeescript - How to get for loop to loop over newly added elements in an array? - coffeescript

I need to loop over an array and add more elements to that array if needed. But coffeescript seems to terminate the loop at old length (where the array ended when the for loop started). I need the loop to loop over newly added elements. How do I fix this?
arr = [1,2,3,4,5]
for x in arr
console.log(x + ">>>" + arr)
if(x < 3)
arr.push(5)
Output on console:
JSFiddle
This doesnt seem to be a problem in js:
arr = [1,2,3,4,5]
for(i=0 ; i<arr.length ; i++){
console.log(arr[i]);
if(arr[i] < 3)
arr.push(5)
}
Output on console:
JSFiddle

Do not mutate the array you're iterating over. Split your algorithm to multiple segments, e.g.:
arr = [1,2,3,4,5]
to_add = []
# 1. Check for new items to add
for x in arr
if x < 3
to_add.push(5)
# 2. Add the new items
arr = arr.concat(to_add)
# 3. Iterate over the array, including the new items
for x in arr
your_thing(x)

Related

How to use for loop to add the previous values in an array in MATLAB?

I have an array like t. It contains the numbers and I would like to add to each number the previous ones. For example: t=[0,2,3,5] and I would like to get tnew=[0,2,5,10]. I tried out this code but it is wrong for sure. (There are 5292 values)
for i=0:5292
t(i)=t(i)+t(i+1)
end
For some array t = [0,2,3,5];, you can just do tnew = cumsum(t).
If you really want to do this in a loop, you need to start from the 2nd index, and keep adding to the value from the previous index
t = [0,2,3,5];
tnew = t;
for ii = 2:numel(t)
tnew(ii) = t(ii) + tnew(ii-1);
end

append element to array in for loop matlab

I have a matrix 10x500 and I want to discard every row which contains in the first 100 elements a value above 6. First I am trying to make an array with all the indexes of the row to discard. Here my code
idx_discard_trials = [];
for i = 1:size(data_matrix,1)
if any(data_matrix(i,1:100)>6)
idx_discard_trials = i;
end
end
However, at the end of the loop I get just the last index, not a list. Does anybody know how to append elements to an array using a for loop?
It's because you keep rewriting a single value, you need to append the values through idx_discard_trials(end+1) = i, for example.
You don't need a loop for this however, try the following:
data_matrix(any(data_matrix(:,1:100) > 6, 2),:) = []

How do I iterate through an imported excel doc?

what I need to do for this code is import a giant (302x11) excel doc, and then ask the user for an input. Then, I need to iterate through each element in the 5th column of the excel array, and if that element matches the user input, save the entire row to a new array. After going through all 302 rows, I need to display the new array.
So far, I have this:
Vin = input('Vin: ');
filename='MagneticCore.xlsx';
sheet=2;
xlRange='B2:L305';
[ndata, text, alldata] = xlsread(filename,sheet,xlRange,'basic');
After this, I'm not sure how to iterate through the alldata array.
alldata is a cell, to select the fifth column you can use alldata{:,5}. Searching in Cells is done this way without iterating
Try it on your own, if you get stuck update your question with code and error message
Daniel R is right, you can do this without iterating through the cell array. Here's how you could iterate through the array if you needed to:
[ndata, text, alldata] = xlsread('Book1.xlsx');
target = 12;
newArray = {};
for r = 1:size(alldata, 1)
% get the element in the fifth column of the current row
e = raw{r,5};
if e == target
% add to newArray
newArray{end + 1} = alldata(r,:);
end
end
% display newArray
for r = 1:size(newArray, 1)
disp(newArray{r})
end

How to work with copy of an array and reset it later

I have an array list with all the predefined data I want to work on.
Then I want to make a copy of that array on which I do the work, i.e. shuffling and then popping one element. Now after the list is empty, I want to reset it, i.e. fill it again with the contents of list.
What I have now is this:
list = [{...}, {...}, {...}]
list2 = list
shuffle = (a) ->
i = a.length
while --i > 0
j = ~~(Math.random() * (i + 1))
t = a[j]
a[j] = a[i]
a[i] = t
a
get_list_item = ->
shuffle(list2)
list2.pop()
reset_list = ->
list2 = list
But after I've popped all the items from list2, reset_list() doesn't reset the list. It's still empty
list2 = list doesn't make a copy of list, it just creates another pointer to the same array. So when you are using pop() the original (and only) array loses elements.
Replace these instructions with list2 = list.slice 0 and it should work like you want it to.

Getting every two elements from an array in CoffeeScript

I want to use every pair of entries in an array. Is there an effective way to do this in CoffeeScript without using the length property of the array?
I am currently doing something like the following:
# arr is an array
for i in [0...arr.length]
first = arr[i]
second = arr[++i]
CoffeeScript has for ... by for adjusting the step size of a normal for loop. So iterate over the array in steps of 2 and grab your elements using an index:
a = [ 1, 2, 3, 4 ]
for e, i in a by 2
first = a[i]
second = a[i + 1]
# Do interesting things here
Demo: http://jsfiddle.net/ambiguous/pvXdA/
If you want, you could use a destructured assignment combined with an array slice inside the loop:
a = [ 'a', 'b', 'c', 'd' ]
for e, i in a by 2
[first, second] = a[i .. i + 1]
#...
Demo: http://jsfiddle.net/ambiguous/DaMdV/
You could also skip the ignored variable and use a range loop:
# three dots, not two
for i in [0 ... a.length] by 2
[first, second] = a[i .. i + 1]
#...
Demo: http://jsfiddle.net/ambiguous/U4AC5/
That compiles to a for(i = 0; i < a.length; i += 2) loop like all the rest do so the range doesn't cost you anything.​