简体   繁体   中英

Julia: Questions about array where the dimension is not determined

I have two beginner's questions:

(1) I want to reshape an array, but dimensions come from a vector which can be a variable. For example,

A = ones(120,1)
b = [2,3,4,5]

I can write

C = reshape(A,2,3,4,5)

But in case b can vary, I want something like

C = reshape(A,b)

This code works in Matlab. Is there an analog in Julia?

(2) I want to slice a high-dimensional array, while keeping the dimensions flexible. In the example above, I fix the last dimension:

C[:,:,:,1]
C[:,:,:,2]

etc. The problem is to find an efficient way: For an array of any dimensions, I can always fix the last dimension and extract values.

Any help will be highly appreciated!

(1) C = reshape(A,b...)

(2) EllipsisNotation.jl provides a .. operator, so C[..,1] does what you want.

如果您不想安装软件包,则(2)中有C[ntuple(x->:, ndims(C)-1)..., 1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM