如何确定数组的维数?

时间:2023-01-02 07:22:21

I have a method that uses transpose, but I do not want to apply this operation if the array has less than 2 dimensions. I am wondering how I can do this in ruby.

我有一个使用转置的方法,但是如果数组小于2维,我不想应用这个操作。我想知道如何在ruby中实现这一点。

so for an array like [1,2] -> it should say 1D

and for an array like [[1,2],[1,2]] it should say 2D

Any help appreciated,

任何帮助表示赞赏,

Ted

泰德

6 个解决方案

#1


2  

You could find it recursively:

你可以递归地找到它:

def getDimension(array)
  if array.first.is_a?(Array)
    1 + getDimension(array.first)
  else
    1
  end
end

I know it is a bit crude and there probably someone who is able to make it much nicer, but the general idea is clear.

我知道这有点粗糙,也许有人能把它做得更好,但总的想法很清楚。

#2


1  

Because Ruby arrays can hold anything this is fragile and a hack at best, but you could always just check to see if the nth element is an array, i.e.,

因为Ruby数组可以容纳任何东西,这是脆弱的,也是一种攻击,但是你可以检查第n个元素是否是一个数组,例如,

def is_2d(array)
  array.first.is_a?(Array)
end

I really don't like this, so if there is something better just slap me or downvote me into oblivion.

我真的不喜欢这样,所以如果有更好的事情,就拍拍我,或者让我失望。

#3


1  

Hmm, I might just try #transpose and rescue IndexError, TypeError, but another idea is:

嗯,我可以试试#转置和救援IndexError, TypeError,但另一个想法是

x.map(&:class).uniq == [Array]

#4


1  

How about

如何

a.map{|e| e.is_a?(Array)}.uniq == [true] ? "#{e.size}D" : "1D"

#5


1  

module MultiDArray #this could be a subclass of Array 
                   #if you can modify your callers
 def self.transposable?(array)
    array[0] && array[0][0] 
 end
 def self.dimensions(array)
    return 0 if array.nil?
    return self.dimensions(array[0]) if array[0].is_a?(Array)
    return 1
 end
 def self.dimension_to_s(array)
    "#{dimensions(array)}D"
 end
end


MultiDArray.transposable? #is probably what you're actually looking for.

This is presuming you're using normal ruby arrays and not Matrixes. You've probably got worse data model problems to deal with if the arrays aren't regular, so one of these two methodologies is probably sufficient.

这是假设您使用的是普通的ruby数组,而不是矩阵。如果数组不是规则的,您可能会遇到更糟糕的数据模型问题,因此这两种方法中的一种可能就足够了。

#6


1  

Don't fear errors, just anticipate them:

不要害怕错误,只要预见它们:

a.transpose rescue a

#1


2  

You could find it recursively:

你可以递归地找到它:

def getDimension(array)
  if array.first.is_a?(Array)
    1 + getDimension(array.first)
  else
    1
  end
end

I know it is a bit crude and there probably someone who is able to make it much nicer, but the general idea is clear.

我知道这有点粗糙,也许有人能把它做得更好,但总的想法很清楚。

#2


1  

Because Ruby arrays can hold anything this is fragile and a hack at best, but you could always just check to see if the nth element is an array, i.e.,

因为Ruby数组可以容纳任何东西,这是脆弱的,也是一种攻击,但是你可以检查第n个元素是否是一个数组,例如,

def is_2d(array)
  array.first.is_a?(Array)
end

I really don't like this, so if there is something better just slap me or downvote me into oblivion.

我真的不喜欢这样,所以如果有更好的事情,就拍拍我,或者让我失望。

#3


1  

Hmm, I might just try #transpose and rescue IndexError, TypeError, but another idea is:

嗯,我可以试试#转置和救援IndexError, TypeError,但另一个想法是

x.map(&:class).uniq == [Array]

#4


1  

How about

如何

a.map{|e| e.is_a?(Array)}.uniq == [true] ? "#{e.size}D" : "1D"

#5


1  

module MultiDArray #this could be a subclass of Array 
                   #if you can modify your callers
 def self.transposable?(array)
    array[0] && array[0][0] 
 end
 def self.dimensions(array)
    return 0 if array.nil?
    return self.dimensions(array[0]) if array[0].is_a?(Array)
    return 1
 end
 def self.dimension_to_s(array)
    "#{dimensions(array)}D"
 end
end


MultiDArray.transposable? #is probably what you're actually looking for.

This is presuming you're using normal ruby arrays and not Matrixes. You've probably got worse data model problems to deal with if the arrays aren't regular, so one of these two methodologies is probably sufficient.

这是假设您使用的是普通的ruby数组,而不是矩阵。如果数组不是规则的,您可能会遇到更糟糕的数据模型问题,因此这两种方法中的一种可能就足够了。

#6


1  

Don't fear errors, just anticipate them:

不要害怕错误,只要预见它们:

a.transpose rescue a