Subtracting from each element in array in python - arrays

I've got a sort of array in python, and i'm looking to subtract one from each int in all of it. for example:
arr = [[2,3,4],
[5,6,7],
[8,9,10]]
#this should become this:
arr = [[1,2,3],
[4,5,6],
[7,8,9]]
there's a few ways i've tried to do this
for i in arr:
for j in i:
j-=1 #doesn't work!
I'm aware it would be easier to do this with numpy, but this is for a large project that i'm working on, so implementing numpy would take hours, if not more. Thanks!

So the way that you are doing it is just reassigning the variable j within your loop to one less its original value. However, what you want to do is reassign the value of the array AT the index j to one less its original value. To do this without using numpy, simply loop through all the indexes of the array, and then replace the value:
for i in range(len(arr)):
for j in range(len(arr[i])):
arr[i][j] -= 1
If you're unsure of why this is, look into how variable assignment works in Python.

You could use a nested list comprehension:
arr = [[y - 1 for y in x] for x in arr]

Your solution didn't work, because jis a copy of the value from your array, rather than a pointer to the array item itself.
Below is sample code that works. Essentially, iterate through each location in the array, and modify the original array at that location.
arr = [[2,3,4],
[5,6,7],
[8,9,10]]
for x_idx, x in enumerate(arr):
for y_idx, y in enumerate(x):
arr[x_idx][y_idx] -= 1
print(arr)

Related

What is the fastest way of converting a numpy array to a ctype array?

Here is a snippet of code I have to convert a numpy array to c_float ctype array so I can pass it to some functions in C language:
arr = my_numpy_array
arr = arr/255.
arr = arr.flatten()
new_arr = (c_float*len(arr))()
new_arr[:] = arr
but since the last line is actually a for loop and we all know how notorious python is when it comes to for loops for a medium size image array it takes about 0.2 seconds!! so this one line is right now the bottle neck of my whole pipeline. I want to know if there is any faster way of doing it.
Update
Please note "to pass to a function in C" in the question. To be more specific I want to put a numpy array in IMAGE data structure and pass it to rgbgr_image function. You can find both here
The OP's answer makes 4 copies of the my_numpu_array, at least 3 of which should be unnecessary. Here's a version that avoids them:
# random array for demonstration
my_numpy_array = np.random.randint(0, 255, (10, 10))
# copy my_numpy_array to a float32 array
arr = my_numpy_array.astype(np.float32)
# divide in place
arr /= 255
# reshape should return a view, not a copy, unlike flatten
ctypes_arr = np.ctypeslib.as_ctypes(arr.reshape(-1))
In some circumstances, reshape will return a copy, but since arr is guaranteed to own it's own data, it should return a view here.
So I managed to do it in this weird way using numpy:
arr = my_numpu_array
arr = arr/255.
arr = arr.flatten()
arr_float32 = np.copy(arr).astype(np.float32)
new_arr = np.ctypeslib.as_ctypes(arr_float32)
In my case it works 10 times faster.
[Edit]: I don't know why it doesn't work without np.copy or with reshape(-1). So it would be awesome if anyone can explain.

Is there an idiomatic way to exchange two elements in a cell array?

I know that I can write it like this:
tmp = arr{i}
arr{i} = arr{j}
arr{j} = tmp
But is there a simpler way? For instance, in Python I'd write:
arr[i], arr[j] = arr[j], arr[i]
Standard, idiomatic way:
Use a vector of indices:
arr([i j]) = arr([j i]); %// arr can be any array type
This works whether arr is a cell array, a numerical array or a string (char array).
Not recommended (but possible):
If you want to use a syntax more similar to that in Python (with a list of elements instead of a vector of indices), you need the deal function. But the resulting statement is more complicated, and varies depending on whether arr is a cell array or a standard array. So it's not recommended (for exchanging two elements). I include it only for completeness:
[arr{i}, arr{j}] = deal(arr{j}, arr{i}); %// for a cell array
[arr(i), arr(j)] = deal(arr(j), arr(i)); %// for a numeric or char array
Not to confuse things, but let me another syntax:
[arr{[i,j]}] = arr{[j,i]};
or
[arr{i},arr{j}] = arr{[j,i]};
The idea here is to use comma-separated lists with curly-braces indexing.
Remember that when working with cell-arrays, ()-indexing gives you a sliced cell-array, while {}-indexing extracts elements from the cell-array and the return type is whatever was stored in the specified cell (when the index is non-scalar, MATLAB returns each cell content individually as a comma-separated list).

can the keyword "foreach" get the array parameter?

I am a beginner of the scala. many concepts aren't clear for me.
I want to ask that if the foreach can get (or control) the array parameter ?
for example:
val array = Array.ofDim[Double](2,6)
I can use for to control the array's parameter, like
for( i <- 0 until 2){
for(j <- 0 until 6){
......... }}
I can use i,j control and get the parameter. Is "foreach" can do that??
(I know foreach can do things without the parameter, but I just want to ask if it can get
the array parameter?)
thank you :)
I'm not exactly sure what you're asking. If you're asking how to loop over the full array item-by-item, then you can do that without nesting for comprehensions. This will print the contents of your 2D array:
for (row <- array; col <- row) println(col)
If you're asking how to loop over all the indices of the array (I think maybe that's what you mean when you say parameter), then you can use the indices property:
for (i <- array.indices; j <- array(i).indices) println(array(i)(j))
If you're just trying to do indexOf across a 2D array, you can probably just reuse the solution for indexOf for 2D Vector in Scala. This is how I'd adapt it to your code:
val array = Array.tabulate(2,6)(_*6+_)
val searchValue = 8
val indices: Option[(Int, Int)] =
(for {
i <- array.indices
j <- array(i).indices
if (array(i)(j) == searchValue)
} yield (i, j)).headOption
If none of the values match, you'll get indices = None. In this case there is a match, so you get indices = Some((1,2)), because 8 is at array(1)(2).
(Note: Unlike indexOf, using a for comprehension does not short-circuit as soon as it finds the solution. If you make the index ranges lazy by calling .iterator, .view or .toStream then you'll get the expected short-circuiting behavior.)
U can use the function [ indexOf(elem: T): Int ] that will return the position
Scala Array

Saving individual arrays from a loop in VBA

So I have an array x(i,j) which is inside a loop k = 1 to n, where n is set by the user. It updates every time I go to the next k. I was wondering if it's possible to save each of the arrays created in the loop to the memory so I can recall them at another time?
So for example, I was thinking I could do something along the lines of:
For k = 1 to n
...
SavedArray(k) = x(i,j)
...
next k
So I could recall, say for example, SavedArray(58) which would have been the 58th iteration of the loop.
Is this feasible?
Thanks for your time.
It depends what is X, and what you wish to save.
If X(i,j) is just Integer so just make a new array and save it : MyArr(k) = X(i,j)
If X(i,j) is array (so X is 2D of 1D array ~ 3D) -you can use Array of Arrays to save it.
To simplify the solution you can create a new type, let say:
Public Type typeArray
X(1 To 10) As Integer
Y(1 To 10) As Integer
End Type
And Declare:
Public SavedArray(1 To 10) As typeArray
Now you can use it as you wish.

How to delete an element from an array in D

Concatenating an element x to an array items is easy in D, it's as if it were an array list:
arr ~= x;
but how do I remove an element at index i from items?
(Caveat: If I remove an element and then add a new element, the array must not be reallocated. So a simple slice won't work.)
Update:
Based on CyberShadow's answer about using assumeSafeAppend, I wrote this code:
static void removeAt(T)(ref T[] arr, size_t index)
{
foreach (i, ref item; arr[index .. $ - 1])
item = arr[i + 1];
arr = arr[0 .. $ - 1];
arr.assumeSafeAppend();
}
However, the problem happens when you have something like:
auto superArr = [0, 1, 2, 3, 4]; //Must not be modified
auto arr = superArr[0 .. $ - 1];
writeln(superArr);
arr.removeAt(0); //Should copy the slice and modify the copy
writeln(superArr); //but obviously doesn't
The base array of slice should not be modified if an element is removed from the slice; instead, the slice needs to be copied.
But I have no way of knowing if an array is a slice of a bigger array... so that doesn't work.
Any suggestions?
Copying my answer on digitalmars.D (thanks for forwarding):
As has been mentioned, std.algorithm.remove can be of help. You may want to look at three of its capabilities in particular: (a) remove multiple offsets in one pass, e.g. remove(a, 0, 4) removes the first and fifth element, (b) you can remove subranges, e.g. remove(a, tuple(1, 3)) removes the second through fourth element, and (c) if you don't care about the order in which elements are left after removal you may want to look into unstable remove, which does considerably less work.
Andrei
(Caveat: If I remove an element and then add a new element, the array must not be reallocated. So a simple slice won't work.)
The assumeSafeAppend function will tell the runtime not to reallocate the array when appending to it (i.e. it is an affirmation from the user that there aren't other slices which might be stomped by an append).
remove from std.algorithm does an in-place remove. If you're using std.container, there's also Array.linearRemove.
Well if order is of no importance you can copy the last element to the location of removal then reduce the array length by one.
If you just want to remove the first or last elements use slices:
array = array [1..$]
array = array [0..$-1]
Or a general way which works for a middle one as well:
array = array [0..unlucky] ~ array [unlucky+1..$]
If the elements aren't basic elements such as structs, floats, ints then arrays are implicitly arrays of pointers and this is an efficient operation.
There's no automated way of doing this, you'll have to shuffle the array items along, reset .length and then catenate.

Resources