使用1D数组在matlab中创建一个列表?(Creating a one column table in matlab using a 1D array?)
我试图将1D数组保存到MATLAB中的表中。 我希望将数据保存在一列中,包含5行数据,而不是5列包含一行数据(如下所示)。
testarray = [1:5];
testarray =
1 2 3 4 5
t=table(testarray);
t=array2table(testarray)
t =
testarray1 testarray2 testarray3 testarray4 testarray5
__________ __________ __________ __________ __________
1 2 3 4 5
我希望输出看起来像:
t =
testarray
_________
1
2
3
4
5
如果有人知道我怎么做这项工作,请告诉我!
I am trying to save a 1D array into a table in MATLAB. I would like the data to be saved in one column, with 5 rows of data, not 5 columns with one row of data (shown below).
testarray = [1:5];
testarray =
1 2 3 4 5
t=table(testarray);
t=array2table(testarray)
t =
testarray1 testarray2 testarray3 testarray4 testarray5
__________ __________ __________ __________ __________
1 2 3 4 5
What I would like the output to look like:
t =
testarray
_________
1
2
3
4
5
If anyone has any idea of how I can make this work, please let me know!
原文:https://stackoverflow.com/questions/40637717
更新时间:2020-02-28 05:32
最满意答案
MATLAB的table对象是面向行的,MATLAB不对假设的矢量情况下的数据形状做出假设。
因为[1:5]是一个行向量:
>> [1:5]
ans =
1 2 3 4 5
MATLAB将此视为一行数据。
因为您需要面向列的数据,所以需要转置此向量:
>> [1:5].'
ans =
1
2
3
4
5
为了让MATLAB将其视为单个变量(列):
>> testarray = [1:5];
t = table(testarray.')
t =
Var1
____
1
2
3
4
5
MATLAB's table objects are row-oriented, and MATLAB makes no assumptions about the shape of your data for the ambiguous vector case.
Because [1:5] is a row vector:
>> [1:5]
ans =
1 2 3 4 5
MATLAB will treat this as one row of data.
Because you want column-oriented data, you will need to transpose this vector:
>> [1:5].'
ans =
1
2
3
4
5
In order for MATLAB to treat it as a single variable (column):
>> testarray = [1:5];
t = table(testarray.')
t =
Var1
____
1
2
3
4
5
2016-11-16
相关问答
最好的方法是将整个2d数组传递给function2()以及选择列的参数。 然后沿轴重复。 for (int i=0; i<16; i++) // For each column of the 2D array "a"
{
function2( a , i );
}
void function2(float b[Y][X] , size_t col )
{
for( size_t i = 0 ; i < Y ; i++ )
b[i][col] = ...
}
...
使用np.ravel (1D视图)或np.flatten (对于1D拷贝)或np.flat (对于1D迭代器): In [12]: a = np.array([[1,2,3], [4,5,6]])
In [13]: b = a.ravel()
In [14]: b
Out[14]: array([1, 2, 3, 4, 5, 6])
请注意, ravel()返回一个view 。 所以修改b也修改a 。 当一维元素在内存中是连续的时, ravel()返回一个view ,但是如果例如使用非单位步
...
你说你的代码生成了一个29686 X 29686矩阵,但是你正在做等式中的元素操作。 这意味着无论是flux还是A_lambda胸围都是29686 X 29686。只需切割那些尺寸! 假设其中一个是29686 X 29686 funred = flux(:,1)*10.^(0.4*A_lambda(:,1));
只要删除不是矩阵的那个(:,1) 。 如果它们都是矩阵,那么你不能这么做,因为flux*...需要整个矩阵运行。 You say that your code generates a 2
...
你只需要2个循环。 对于每个列遍历所有行并总结内容。 将总和写在适当的col索引处。 然后在每列重置总和之后。 您还需要返回带有总和的数组。 所以我改变了返回值: 如果您使用有意义的名称调用索引变量,它也会有所帮助。 public static int[] sumcolumn(int[,] mat)
{
int[] sumcol = new int[mat.GetLength(1)];
for (int col = 0; col < mat.GetLength(1); col++
...
MATLAB的table对象是面向行的,MATLAB不对假设的矢量情况下的数据形状做出假设。 因为[1:5]是一个行向量: >> [1:5]
ans =
1 2 3 4 5
MATLAB将此视为一行数据。 因为您需要面向列的数据,所以需要转置此向量: >> [1:5].'
ans =
1
2
3
4
5
为了让MATLAB将其视为单个变量(列): >> testarray = [1:5];
t
...
当涉及到相同类型的串联时, string对象的行为与任何其他数据类型( double , char等)一样。 只要您希望结果也是string对象,请使用常规连接。 result = [strings(1, 4); strings(1, 4)];
或者您可以使用cat或vertcat更明确 result = cat(1, strings(1, 4), strings(1, 4));
result = vertcat(strings(1, 4), strings(1, 4));
或者,您可以使用索
...
为了使用像这样的2D数组修改每个通道,您需要获取每个通道(作为2D数组),然后使用2D逻辑阵列将所需的像素替换为所需的值,然后将其分配回数组。 green = CDM(:,:,2);
green(CD) = 255;
CDM(:,:,2) = green;
或者,您可以执行以下操作来向量化问题。 replace_color = [0 255 255];
CDM = bsxfun(@times, ~CD, CDM) + bsxfun(@times, CD, reshape(replace_colo
...
如果我没弄错的话,您可以只取A列和B列的乘积之和,然后除以Count列的总和: =SUMPRODUCT(A2:A6, B2:B6) / SUM(B2:B6)
请注意,使用手写的扩展公式产生相同的结果: =AVERAGE(1,2,3,3,4,4,4,5,5,5,5,5)
If I'm not mistaken, you can just take the sum of product of columns A and B, then divide by the sum of the Count
...