MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   在MATLAB中如何将矩阵的所有值设置为字符串? (https://www.labfans.com/bbs/showthread.php?t=23508)

poster 2019-12-10 20:41

在MATLAB中如何将矩阵的所有值设置为字符串?
 
我有一个MATLAB矩阵,即1000x4,可以用作函数的输入。我需要添加一个包含特定字符串的新列。那么,如何在所有值均为“ TEST”的地方创建新列?



[B]回答:[/B]

由于不清楚您想要什么,因此有一些选择:
[LIST][*]要制作每行为'TEST'的1000×4矩阵,可以使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"]REPMAT[/URL]函数:

M = repmat('TEST',1000,1);[*]要将'TEST'添加到1000 x 4字符矩阵的每一行的末尾,可以使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/strcat.html"]STRCAT[/URL]函数:

M = repmat('a',1000,4); %# Sample matrix filled with 'a' M = strcat(M,'TEST'); %# Append 'TEST' to each row of M[*]如果您的1000 x 4矩阵是数字数组而不是字符数组,则必须使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/br04bw6-98.html"]单元格数组[/URL]来组合不同类型的数据。这是您可以执行此操作的一种方法:

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'[/LIST]
[url=https://stackoverflow.com/questions/2975648]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 01:08

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.