I had to read data from a file and store all data in a one dimensional array. However, Some data I have to store in a matrix (2 dimensional array) How Can I do this?
我必须从文件中读取数据并将所有数据存储在一维数组中。但是,有些数据我必须存储在一个矩阵(二维数组)中我该怎么做?
For example if my data is 1x7
array [1,2,3,1,5,2,8]
and the first to 6th belong to a matrix 2x3
how can I store in a new array variable?
例如,如果我的数据是1x7数组[1,2,3,1,5,2,8]并且第一个到第6个属于矩阵2x3,我如何存储新的数组变量?
1 个解决方案
#1
4
Suppose that your 7-element array is called array7
, then the following expression should return a 2x3
array containing the first 6 elements of array7
假设你的7元素数组被称为array7,那么下面的表达式应该返回一个包含array7的前6个元素的2x3数组
reshape(array7(1:6),[2,3])
If that puts the elements into the new array in the wrong order, try
如果以错误的顺序将元素放入新数组中,请尝试
reshape(array7(1:6),[2,3],order=[2,1])
Note that I've used a named optional argument in the second version, there is another optional argument (pad
) which would be, by default, the 3rd argument to reshape
.
请注意,我在第二个版本中使用了一个命名的可选参数,还有另一个可选参数(pad),默认情况下,它将是reshape的第三个参数。
#1
4
Suppose that your 7-element array is called array7
, then the following expression should return a 2x3
array containing the first 6 elements of array7
假设你的7元素数组被称为array7,那么下面的表达式应该返回一个包含array7的前6个元素的2x3数组
reshape(array7(1:6),[2,3])
If that puts the elements into the new array in the wrong order, try
如果以错误的顺序将元素放入新数组中,请尝试
reshape(array7(1:6),[2,3],order=[2,1])
Note that I've used a named optional argument in the second version, there is another optional argument (pad
) which would be, by default, the 3rd argument to reshape
.
请注意,我在第二个版本中使用了一个命名的可选参数,还有另一个可选参数(pad),默认情况下,它将是reshape的第三个参数。