开发者

In MATLAB how can I set all the values of a matrix to string?

I have a MATLAB matrix, that is 1000x4, to use as an input for a function. I need to add a new column that contains a certain string. So how can I make a new column 开发者_Python百科where all the values are 'TEST'?


Since it's a little unclear what you want, here are some options:

  • To make a 1000-by-4 matrix where each row is 'TEST', you can use the function REPMAT:

    M = repmat('TEST',1000,1);
    
  • To add 'TEST' to the end of each row of a 1000-by-4 matrix of characters, you can use the function STRCAT:

    M = repmat('a',1000,4);  %# Sample matrix filled with 'a'
    M = strcat(M,'TEST');    %# Append 'TEST' to each row of M
    
  • If your 1000-by-4 matrix is a numeric array instead of an array of characters, you will have to use cell arrays to combine the different types of data. Here's one way you can do this:

    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'
    


A matrix cannot contain a string (like 'TEST'). You need to use a cell array


If this is an existing matrix M of cell strings,

M(:,end+1) = {'TEST'};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜