如何将矩阵转换为数组数组?

时间:2022-11-19 21:45:32

In How to convert an array of array into a matrix? we learned how to convert an array of arrays to a matrix. But what about the other way around? How do we go from input to output, as shown below?

在如何将数组数组转换为矩阵?我们学习了如何将数组数组转换为矩阵。但另一种方式呢?我们如何从输入到输出,如下所示?

input = [1 2 3; 4 5 6; 7 8 9]
output = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

2 个解决方案

#1


8  

If you want to make a copy of the data then:

如果要复制数据,则:

[input[i, :] for i in 1:size(input, 1)]

If you do not want to make a copy of the data you can use views:

如果您不想复制数据,可以使用视图:

[view(input, i, :) for i in 1:size(input, 1)]

After some thought those are alternatives using broadcasting:

经过一番思考,这些是使用广播的替代品

getindex.([input], 1:size(input, 1), :)
view.([input], 1:size(input, 1), :)

#2


2  

I add one alternative too:

我也添加了一个替代方案:

mapslices(x->[x], input,2)

Edit:

Warning! Now I see that mapslices return 3x1 matrix! (you could change it: mapslices(x->[x], input,2)[:,1])

警告!现在我看到mapslices返回3x1矩阵! (你可以改变它:mapslices(x - > [x],input,2)[:,1])

I am unsatisfied. I don't like any solution we find yet. They are too complicated (think for example how to explain it to children!).

我不满意。我不喜欢我们发现的任何解决方案。它们太复杂了(想想如何向孩子解释它!)。

It is also difficult to find function like mapslices in doc too. BTW there is non-exported Base.vect function which could be used instead of anonymous x->[x].

在doc中也很难找到像mapslices这样的函数。 BTW有非导出的Base.vect函数,可以用来代替匿名x - > [x]。

I was thinking that sometimes is more clever to use bigger hammer. So I tried to find something with DataFrames

我当时认为使用更大的锤子有时更聪明。所以我试着用DataFrames找到一些东西

julia> using DataFrames
julia> DataFrame(transpose(input)).columns
3-element Array{Any,1}:
 [1, 2, 3]
 [4, 5, 6]
 [7, 8, 9]
  1. unfortunately there is not DataFrame.rows
  2. 遗憾的是,没有DataFrame.rows

  3. result's type is Array{Any,1}
  4. 结果的类型是数组{Any,1}

  5. I don't think it could be very quick
  6. 我认为它不会很快

I hope Julia could get us better solution! :)

我希望朱莉娅能给我们更好的解决方案! :)

#1


8  

If you want to make a copy of the data then:

如果要复制数据,则:

[input[i, :] for i in 1:size(input, 1)]

If you do not want to make a copy of the data you can use views:

如果您不想复制数据,可以使用视图:

[view(input, i, :) for i in 1:size(input, 1)]

After some thought those are alternatives using broadcasting:

经过一番思考,这些是使用广播的替代品

getindex.([input], 1:size(input, 1), :)
view.([input], 1:size(input, 1), :)

#2


2  

I add one alternative too:

我也添加了一个替代方案:

mapslices(x->[x], input,2)

Edit:

Warning! Now I see that mapslices return 3x1 matrix! (you could change it: mapslices(x->[x], input,2)[:,1])

警告!现在我看到mapslices返回3x1矩阵! (你可以改变它:mapslices(x - > [x],input,2)[:,1])

I am unsatisfied. I don't like any solution we find yet. They are too complicated (think for example how to explain it to children!).

我不满意。我不喜欢我们发现的任何解决方案。它们太复杂了(想想如何向孩子解释它!)。

It is also difficult to find function like mapslices in doc too. BTW there is non-exported Base.vect function which could be used instead of anonymous x->[x].

在doc中也很难找到像mapslices这样的函数。 BTW有非导出的Base.vect函数,可以用来代替匿名x - > [x]。

I was thinking that sometimes is more clever to use bigger hammer. So I tried to find something with DataFrames

我当时认为使用更大的锤子有时更聪明。所以我试着用DataFrames找到一些东西

julia> using DataFrames
julia> DataFrame(transpose(input)).columns
3-element Array{Any,1}:
 [1, 2, 3]
 [4, 5, 6]
 [7, 8, 9]
  1. unfortunately there is not DataFrame.rows
  2. 遗憾的是,没有DataFrame.rows

  3. result's type is Array{Any,1}
  4. 结果的类型是数组{Any,1}

  5. I don't think it could be very quick
  6. 我认为它不会很快

I hope Julia could get us better solution! :)

我希望朱莉娅能给我们更好的解决方案! :)