Slicing NumPy 3d array - numpy-ndarray

I am learning NumPy. I created 3d array like this:
a = np.array([[[1,2], [3,4]], [[5,6], [7,8]]])
a
a
# array([[[1, 2],
# [3, 4]],
# [[5, 6],
# [7, 8]]])
I am trying to access values 3 to 6 as under:
a[0,1] # array([3, 4])
a[1,0] # array([5, 6])
a[0,1][1,0] # IndexError: too many indices for array
Is this possible through one-liner slice?

Try this:
import numpy as np
a = np.array([[[1,2,3], [4,5,6], [7,8,9]]])
tmp = a.flatten()
tmp[2:6]

Related

matrixflip(m,d) where m is the matrix and d can be either h or v which are horizontal and vertical respectively

I am trying to write a python code for function which should return the matrix flipped horizontally and vertically
Am new to python
def matrixflip(myl,'v'):
output = list(myl[::-1])
return output
myl = [[1,2],[3,4]]
myl
[[1, 2], [3, 4]]
matrixflip(myl,'h')
[[2, 1], [4, 3]]
myl
[[1, 2], [3, 4]]
matrixflip(myl,'v')
[[3, 4], [1, 2]]
myl
[[1, 2], [3, 4]]
import copy
def matrixflip(l,char):
myl = copy.deepcopy(l)
if char == 'h':
for i in range(len(myll)):
myl[i].reverse()
if char == 'v':
i,j = 0, len(myl)-1
while(i<j):
myl[i], myl[j] = myl[j], myl[i]
i+=1
j-=1
return(myl)
Copy is used because original matrix value changed.
So don't change original list.

Convert a nested array to a matrix in Ruby?

When converting a nested array to a matrix in Ruby, the matrix ends up with an extra [] around the values, compared to simply creating a matrix from scratch.
> require 'matrix'
> matrix1 = Matrix[[1,2,3],[4,5,6],[7,8,9]]
> p matrix1
=> Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
> nested_array = [[1,2,3],[4,5,6],[7,8,9]]
> matrix2 = Matrix[nested_array]
> p matrix2
=> Matrix[[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]
Is there a way to avoid the extra square brackets when building from an array?
matrix2 = Matrix[*nested_array]
p matrix2
=> Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The asterisk (*) there is called the "splat operator," and it essentially can be used to treat an array (nested_array in this case) as if it weren't an array, but rather as if its elements were individual elements/arguments.

How to convert arrays of numbers into a matrix in Ruby

I have a two simple arrays of numbers, representing the cartesian position of an object.
a = [3, 4]
b = [8, 5]
I want to check if "a" and "b" are beside each other. I would like to convert the two into a matrix and perform a subtractions of the two positions, and then check if the absolute value of either element is "1".
Is there a way to do this?
You're getting the uninitialized constant error because you first need:
require 'matrix'
Then you could just:
Matrix[a,b]
Sample interactive output:
irb(main):011:0> require 'matrix'
=> true
irb(main):012:0> Matrix[a,b]
=> Matrix[[3, 4], [8, 5]]
I don't think using Matrix class methods is justified here. The only method that would be marginally useful is Matrix#-, but to use that you need to convert your arrays to Matrix objects, apply Matrix#-, then convert the resultant matrix object back to an array to determine if the absolute value of any element equals one (whew!). I'd just do this:
def adjacent?(a,b)
a.zip(b).any? { |i,j| (i-j).abs == 1 }
end
adjacent?([3, 4], [8, 5]) #=> true
adjacent?([3, 7], [8, 5]) #=> false
adjacent?([3, 7], [2, 5]) #=> true
For the first example:
a = [3, 4]
b = [8, 5]
c = a.zip(b)
#=> [[3, 8], [4, 5]]
c.any? { |i,j| (i-j).abs == 1 }
#=> true
The last statements determines if either of the following is true.
(3-8).abs == 1
(4-5).abs == 1

How to access a nested element, passing array with coordinates

Is there any short way to access an element of a nested array, passing the array with coordinates? I mean something like:
matrix = [[1,2,3,4],[5,6,7,8]]
array = [1,1]
matrix [array]
# => 6
I just wonder if there is a shorter version than:
matrix [array[0]][array[1]]
I believe you want to use the Matrix class:
require 'matrix'
arr = [[1,2,3,4],[5,6,7,8]]
matrix = Matrix[*arr] #=> Matrix[[1, 2, 3, 4], [5, 6, 7, 8]]
matrix[1,1] #=> 6
matrix.row(1) #=> Vector[5, 6, 7, 8]
c = matrix.column(1) #=> Vector[2, 6]
c.to_a #=> [2, 6]
m = matrix.transpose #=> Matrix[[1, 5], [2, 6], [3, 7], [4, 8]]
m.to_a #=> [[1, 5], [2, 6], [3, 7], [4, 8]]
array.inject(matrix, :fetch)
# => 6
matrix[1][1]
should equal 6. matrix[1] is the 2nd array, matrix[1][1] is the second element in that array.

Split array up into n-groups of m size? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Need to split arrays to sub arrays of specified size in Ruby
I'm looking to take an array---say [0,5,3,8,21,7,2] for example---and produce an array of arrays, split every so many places. If the above array were set to a, then
a.split_every(3)
would return [[0,5,3],[8,21,7][2]]
Does this exist, or do I have to implement it myself?
Use Enumerable#each_slice.
a.each_slice(3).to_a
Or, to iterate (and not bother with keeping the array):
a.each_slice(3) do |x,y,z|
p [x,y,z]
end
a = (1..6).to_a
a.each_slice(2).to_a # => [[1, 2], [3, 4], [5, 6]]
a.each_slice(3).to_a # => [[1, 2, 3], [4, 5, 6]]
a.each_slice(4).to_a # => [[1, 2, 3, 4], [5, 6]]

Resources