poster
2019-12-10, 20:41
我有一个MATLAB矩阵,即1000x4,可以用作函数的输入。我需要添加一个包含特定字符串的新列。那么,如何在所有值均为“ TEST”的地方创建新列?
回答:
由于不清楚您想要什么,因此有一些选择:
要制作每行为'TEST'的1000×4矩阵,可以使用REPMAT (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html)函数:
M = repmat('TEST',1000,1);
要将'TEST'添加到1000 x 4字符矩阵的每一行的末尾,可以使用STRCAT (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/strcat.html)函数:
M = repmat('a',1000,4); %# Sample matrix filled with 'a' M = strcat(M,'TEST'); %# Append 'TEST' to each row of M
如果您的1000 x 4矩阵是数字数组而不是字符数组,则必须使用单元格数组 (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/br04bw6-98.html)来组合不同类型的数据。这是您可以执行此操作的一种方法:
M = rand(1000,4); %# A matrix of random numeric values M = num2cell(M,2); %# Put each row of M in a cell, making %# a 1000-by-1 cell array M(:,2) = {'TEST'}; %# Add a second column to the cell array, %# where each cell contains 'TEST'
更多&回答... (https://stackoverflow.com/questions/2975648)
回答:
由于不清楚您想要什么,因此有一些选择:
要制作每行为'TEST'的1000×4矩阵,可以使用REPMAT (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html)函数:
M = repmat('TEST',1000,1);
要将'TEST'添加到1000 x 4字符矩阵的每一行的末尾,可以使用STRCAT (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/strcat.html)函数:
M = repmat('a',1000,4); %# Sample matrix filled with 'a' M = strcat(M,'TEST'); %# Append 'TEST' to each row of M
如果您的1000 x 4矩阵是数字数组而不是字符数组,则必须使用单元格数组 (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/br04bw6-98.html)来组合不同类型的数据。这是您可以执行此操作的一种方法:
M = rand(1000,4); %# A matrix of random numeric values M = num2cell(M,2); %# Put each row of M in a cell, making %# a 1000-by-1 cell array M(:,2) = {'TEST'}; %# Add a second column to the cell array, %# where each cell contains 'TEST'
更多&回答... (https://stackoverflow.com/questions/2975648)