Removing multiple elements from array by index in Ruby [duplicate] - arrays

This question already has answers here:
Delete contents of array based on a set of indexes
(4 answers)
Closed 5 years ago.
I have an array. For example,
x = [1,2,3,4,5]
I know the command
x.delete_at(i)
will delete the element at index i from the array. But from what I've read, it can only handle one argument.
Let's say I have a variable that stores the indexes I wish to remove from x. For example,
y = [0,2,3]
My question: Is it possible to remove multiple elements from an array using another array that stores in the indexes you wish to delete at?
In essence, something like
x.delete_at(y)
Thanks! :)

You can use reject with with_index:
x.reject.with_index { |e, i| y.include? i } #=> [2, 5]

Related

Python numpy: Selecting array entries based on input array [duplicate]

This question already has answers here:
Getting the indices of several elements in a NumPy array at once
(5 answers)
Closed 2 years ago.
Assume I have an array:
a = np.array([1,2,3,4,5])
Now I want to find the indices of elements in this array corresponding to the values given by another array input:
input = np.array([2,4,5])
The expected result should be:
result = [1,3,4]
A boolean mask, which is true for element indices 1,3,4 would also be fine.
I do not want to use looping to solve this. I assume that a possible solution has to do with the numpy where() function, but using this one, I am only able to compare the entries of array a with one element of array input at a time. Because the length of input might differ, I cannot really use this approach. Do you have any other ideas?
Thanks in advance.
np.where(np.in1d(a, inp))[0]
or:
np.isin(a, inp).nonzero()[0]
or as suggested here:
sorter = np.argsort(a)
sorter[np.searchsorted(a, inp, sorter=sorter)]
output:
[1 3 4]
np.where(np.in1d(a, inp))[0] np.where(np.in1d(a, inp))[0]

how to access an element of an n-D matrix where index comes from a mathematical operation [duplicate]

This question already has answers here:
MATLAB: Accessing an element of a multidimensional array with a list
(2 answers)
Use a vector as an index to a matrix
(3 answers)
Closed 5 years ago.
How can I access an element of an n-D matrix where index comes from a mathematical operation in Matlab?
For example I have a 4D Matrix called A.
I want to access element 1,1,1,1 which results from (3,4,5,6) - (2,3,4,5)
Is there any way I can do this assuming that the array can be any dimension d and that the array from subtraction will always be d elements long?
One possible way would be to utilise the fact that MATLAB can use linear indexing for any n-dimensional array as well as row-column type indexing. Then you just have to calculate the linear index of your operation result.
There may be a more elegant way to do this but if x is the array holding the result of your operation, then the following works
element = A(sum((x-1).*(size(A).^[0:length(size(A))-1]))+1);
The sub2ind function feels like it should help here, but doesn't seem to.
Another approach is to converting to a cell array, then to a comma-separated list:
A = rand(3,4,5,6); % example A
t = [2 1 3 4]; % example index
u = num2cell(t);
result = A(u{:});

Extract one dimension from a multidimensional array [duplicate]

This question already has answers here:
On shape-agnostic slicing of ndarrays
(2 answers)
Closed 6 years ago.
Suppose A is multi-dimensional array (MDA) of size 3,4,5 and B is another MDA of size 3,4,5,6.
I know A(1,:,:) or B(1,:,:,:) can both extract their elements along the first dimension.
I now need to write a general program to extract the k-th dimension from a MDA without knowing its size.
For example, the MDA C has 6 dimension: 4,5,6,7,8,9 and I want an extraction C(:,:,k,:,:,:).
Sometimes, the MDA 'D' has 4 dimension: 3,4,5,6 and I want another extraction D(k,:,:,:).
That is, my problem is the numbers of colon is varying because of the dimension.
Thanks in advance
You can use string arrays to index the array dynamically:
function out = extract(arr,dim,k)
subses = repmat({':'}, [1 ndims(arr)]);
subses(dim) = num2cell(k);
out = arr(subses{:});
where dim is the dimension in which you want to select and k is an index within that dimension.
I have used a code from this answer:
https://stackoverflow.com/a/27975910/3399825

F# - assigning element in (jagged) 2d-array sets several elements [duplicate]

This question already has an answer here:
Array.create and jagged array
(1 answer)
Closed 6 years ago.
I'm experiencing some behavior in f# that i do not understand. I was trying to create a 2d table using nested arrays. The sub-arrays will have the same length, so I could have used Array2D. However later on I will need the table rows as normal arrays, so in order to avoid conversion from a multidimensional to a regular array I want to represent the table as a jagged array.
The following code is an example of how I initialize and assign elements in the table.
let table = Array.create 3 (Array.zeroCreate<int> 2);;
table.[0].[0] <- 1;;
I would expect this piece of code to set the first element in the first row. However, what it actually does is setting the first element in all three rows.
table;;
val it : int [] [] = [|[|1; 0|]; [|1; 0|]; [|1; 0|]|]
Why does table.[0].[0] set the first element in all three sub-arrays? I tried finding the memory addresses of the sub-arrays using System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement, and it seems to me that they are different, so they three rows are not the same array. What is going on here?
When you use array.create, it takes as an argument an object, not a function.
As a result, each element of the jagged array is a reference to the same array, so you get the observed behaviour.
Just use a function other than array.create to make the array

Delete all occurrences of value in Swift Array [duplicate]

This question already has answers here:
Remove matched item from array of objects?
(5 answers)
Closed 7 years ago.
So I have an array of Int's and I am trying to create a function to delete a certain value from the array. However, the removeAtIndex() function only deletes the first occurrence and the removeLast() only removes the last. I tried enumerating through the array but I end up with an Array index out of range error, possible due to the reshifting that occurs when deleting an item from the array.
for (index, value) in connectionTypeIDs.enumerate() {
if (value == connectionTypeToDelete){
connectionTypeIDs.removeAtIndex(index)
}
}
Any ideas on how to accomplish this?
The quickest way is to use filter. I don't know is that answer your question but you can have a look on this:
// remove 1 from array
arr = arr.filter{$0 != 1}

Resources