Ruby: Compare if array contains other array [duplicate] - arrays

This question already has answers here:
How to determine if one array contains all elements of another array
(8 answers)
Closed 4 years ago.
how can i compare if an array of strings contains a smaller array of strings in Ruby?
e.g.
a=["1","2","3","4","5"]
b=["2","3"]
now i want to check if a contains b and get true/false
Thanks.

The most common approach would be to
(b - a).empty?
It has issues with unique elements, though. To detect whether a includes all elements from b, one should:
a_copy = a.dup
b.all? { |e| a_copy.delete e }
# or
b.all?(&a_copy.method(:delete))

Related

Fortran operations on multidimensional arrays - sum example [duplicate]

This question already has answers here:
Sum of a two dimensional array
(1 answer)
sum only on certain dimension
(1 answer)
Closed 4 years ago.
I am trying to figure how to do simple algebraic operations on a subset of elements in a multidimensional array and assign the result to another array with the same dimensions.
program hello
IMPLICIT NONE
REAL,DIMENSION(10,10,2,1) :: tmp1=2
REAL,DIMENSION(10,10,2,1) :: tmp2=1
INTEGER,DIMENSION(2) :: myind=(/1,2/)
This operations returns only one value
Print *, sum(tmp1(:,:,itree,:))
While this returns the correct results, however I would expect the same result than the second operation, maybe I am thinking in the R way.
Print *, tmp1(:,:,1,:)+tmp1(:,:,2,:)
end program Hello
How do I achieve the result of the second operation and assign it to another multidimensional array? Imagine my index is large so I can't type out each single term in the operation.

testing if an array is not empty in numpy [duplicate]

This question already has answers here:
How can I check whether a numpy array is empty or not?
(4 answers)
Closed 5 years ago.
I wrote the following code:
import numpy as np
a = np.array([0.1])
assert a!=[]
this returned false. Why is this the case? How do I check an array is non-empty?
Well, [] is an empty Python list object, while np.array([0.1]) is a numpy array. You can't really compare the two as you have done; a better way would be to access the size property of the numpy array (also mentioned here).
a = np.array([0.1])
assert a.size != 0

Index out of range in multidimensional array in Swift [duplicate]

This question already has answers here:
Swift 3 2d array of Int
(2 answers)
Closed 5 years ago.
var tri = [[Int]]();
tri[0][0] = 321;
This code causes this error:
fatal error: Index out of range
What's wrong?
You're accessing the first element of the first subarray of the array. But your array doesn't contain any subarrays, so it crashes.

Move array in array of arrays [duplicate]

This question already has answers here:
How to change the position of an array element?
(8 answers)
Closed 6 years ago.
I have an array of arrays like this:
[['a','b','c'],['d','e','f'],['g','h','i'],['j','k','l']]
Now I want to move the last array ['j','k','l'] after array ['a','b','c'] and before array ['d','e','f']. How can I do this?
array.insert(1, array.delete_at(3))
This should do it.

Replacing diagonal elements of square matrix without looping [duplicate]

This question already has answers here:
How to assign values to a MATLAB matrix on the diagonal?
(7 answers)
Closed 8 years ago.
I have a square matrix n x n & I also have a vector which is n x 1. I want to replace the diagonal elements with the values in my vector.
Is there a way of doing this in Matlab without looping?
matrix(1:n+1:end) = vector;
Explanation: if you use a single index into a matrix (that's called linear indexing), Matlab counts the elements down the first column, then second column etc. A step of n+1 thus defines the diagonal.

Resources