matlab中columns怎么用,MatLab:连接数组的2列(MatLab: concatenate 2 columns of an array)

MatLab:连接数组的2列(MatLab: concatenate 2 columns of an array)

我试图导入一个.csv文件来处理MATLAB中的数据,但我遇到了一些麻烦。

该文件包含一个具有相关编号的列,第二列包含一个浮点数(十进制数字用逗号分隔)。

当我用csvread()读取文件时,我获得了一个包含3列的数组(一列具有相关数字,另一列具有浮点数的整数,另一列具有浮点数的小数部分)。

我想要做的下一步是使用它们之间的连接来连接该数组的第二和第三列,这样我就可以组装原始的浮点数。

有没有办法做到这一点? 或者更好的方法来导入那种数据?

这是原始csv文件的示例:

1,1,143526

2,1,143524

3,1,143472

4,1,143413

5,1,143472

6,1,143485

7,1,143556

8,1,143552

9,1,143644

10,1,143559

我需要的是一个数组:

[1 2 3 4 5 6 7 8 9 10]

另一个有这个:

[1.143526 1.143524 1.143472 1.143413 1.143472 1.143485 ...

1.143556 1.143552 1.143644 1.143559]

I am trying to import a .csv file to work with the data inside MATLAB, but I am having some troubles.

The file contains one column with a correlative number, the second column contains a float number (with decimal numbers separated by a comma).

When I read the file with csvread() I obtain an array with 3 columns (one column with the correlative number, another column with the integer number of the float number and another one with the decimal part of the float number).

The next step I would like to do is to concatenate the second and third column of that array using a , between them so I can assemble the original float number.

Is there any way to do that? Or a better way to import that kind of data??

This is an example of the raw csv file:

1,1,143526

2,1,143524

3,1,143472

4,1,143413

5,1,143472

6,1,143485

7,1,143556

8,1,143552

9,1,143644

10,1,143559

And what I need is one array with this:

[1 2 3 4 5 6 7 8 9 10]

And another one with this:

[1.143526 1.143524 1.143472 1.143413 1.143472 1.143485 ...

1.143556 1.143552 1.143644 1.143559]

原文:https://stackoverflow.com/questions/29716406

更新时间:2020-01-09 00:23

最满意答案

您遇到的问题是,通过使用逗号作为十进制数的分隔符,MATLAB(或更好: csvread )假定您有3行:

1 , 1 , 143526

而不是两行,其中第二行是浮点数,根据需要:

1 1.143526

通过将逗号后面的部分除以使其变为0.143526 ,可以容易地组合向量。 您可以找到必须除以的数字

10.^(ceil(log10(x)))

对于143526这给出10^6 ,因此除以此给出0.143526 。 现在我们只需要从第二列添加整数部分。

假设您将CSV文件读取到名为X的矩阵,您可以创建两个向量

corrNumber = X(:,1);

floatNr = X(:,2) + X(:,3) ./ 10.^(ceil(log10(X(:,3))));

如评论中所述,像1.0998这样的1.0998会导致这种方法出现问题。 如果小数位数是固定的,那么在不改变csvread到fscanf情况下解决这个问题的唯一方法是。 然后我们可以通过插入max来获得最大比例因子:

floatNr = X(:,2) + X(:,3) ./ 10.^(ceil(max(log10(X(:,3)))));

The problem you are having is that by using a comma , as separator for decimal numbers, MATLAB (or better: csvread) assumes that you have 3 rows:

1 , 1 , 143526

instead of two rows, where the second one would be a floating point number, as desired:

1 1.143526

The vectors can easily be combined by dividing the part behind the comma such that it becomes 0.143526. You can find the number by which you have to divide by

10.^(ceil(log10(x)))

For 143526 this gives 10^6, so dividing by this gives 0.143526 as desired. Now we just have to add the integer part from the second column.

Assuming you read the CSV file to a matrix called X, you can create the two vectors by

corrNumber = X(:,1);

floatNr = X(:,2) + X(:,3) ./ 10.^(ceil(log10(X(:,3))));

As described in a comment, numbers like 1.0998 lead to problems with this approach. The only way to solve this without changing from csvread to e.g. fscanf is, if the number of decimals is fixed. Then we can get the maximal scaling factor by inserting a max:

floatNr = X(:,2) + X(:,3) ./ 10.^(ceil(max(log10(X(:,3)))));

2015-04-19

相关问答

问题的解决方法是将一个大的三维矩阵M(:,:,j)为一个连续的二维矩阵。 为此,只需使用reshape ,例如: M=reshape(M,size(M,1),[]);

the way the question is put is to convert a big 3-D matrix M(:,:,j) to a concatenated 2-D one. For that just use reshape, for example: M=reshape(M,size(M,1),[]);

您遇到的问题是,通过使用逗号作为十进制数的分隔符,MATLAB(或更好: csvread )假定您有3行: 1 , 1 , 143526

而不是两行,其中第二行是浮点数,根据需要: 1 1.143526

通过将逗号后面的部分除以使其变为0.143526 ,可以容易地组合向量。 您可以找到必须除以的数字 10.^(ceil(log10(x)))

对于143526这给出10^6 ,因此除以此给出0.143526 。 现在我们只需要从第二列添加整数部分。 假设您将CSV文件读取到名为

...

对于数据映射,您可以考虑使用array.map API。 例: var range =

[

[ 'col1.a', 'col2.1', 'c' ],

[ 'col1.b', 'col2.2', '3' ],

[ 'col1.c', 'col2.3', '6' ],

[ 'col1.d', 'col2.4', '9' ],

[ 'col1-e', 'col2.5', '1c' ],

...

copy=x并不真正复制列表( x是一个列表,对吗?)但是copy指向与x相同的列表。 当然, copy.pop()等同于x.pop() ,这不是你想要的。 要复制列表,请copy = x[:] 。 copy=x doesn't really copy the list (x is a list, right?) but make copy to point to the same list as x. Of course, copy.pop() is equivalent to x.pop()

...

好的,所以答案比我想象的要简单得多,我整个上午一直在弄乱,试图找到一个过于复杂的解决方案,问问题然后消失了,我突然想到: ->orWhere('CONCAT(users_data.first_name, " ", users_data.last_name) LIKE ?', '%'.strip_tags($like).'%')

这对我有用,我不确定它是正确的方法,但它确实有效,所以我会留在这里,以防它帮助其他人。 OK so the answer was a lot simpler than I

...

这是一种方式: random_int = randi([0 500],5,10); % example data

y = mat2cell(random_int, ones(1,size(random_int,1)), size(random_int,2)); % split into rows

y = cellfun(@(x) sprintf('%i,', x), y, 'UniformOutput', false); % strings with commas

y = cellfun(@(s)

...

注意a和b都是一维的; 没有轴1连接起来。 你想要vstack : >>> import numpy as np

>>> a = np.array([1,2,3])

>>> b = a.copy()

>>> np.vstack([a,b])

array([[1, 2, 3],

[1, 2, 3]])

或者,您可以先重塑a和b : >>> np.concatenate([a[np.newaxis,:],b[np.newaxis,:]],axis = 0)

array([[1, 2,

...

我这样做: np.column_stack((x1, x2, x3))

对我来说,这更具表现力,做你想要的,并且有一个直观的名字,需要少一个参数。 I'd do it this way: np.column_stack((x1, x2, x3))

To me this is more expressive, does what you want, and has an intuitive name with one less argument required.

你想如何连接它们? 例如,以下两个工作: A = rand(UInt8, 2,2)

B = rand(UInt8, 2,2)

C = [A B]

D = [A ; B]

How exactly do you want to concatenate them? The following, for example, both work: A = rand(UInt8, 2,2)

B = rand(UInt8, 2,2)

C = [A B]

D = [A ; B]

看看这是否适合你 - result = month(strcat(token,cellstr(dateno),names))

这将是一个单元格数组输出。 如果您希望以char类型获取结果,请使用char包装它。 See if this works for you - result = month(strcat(token,cellstr(dateno),names))

which would be a cell array output. If you were looking to get

...