我有一个数字数组,我希望与文本数组结合。
TX = {'a' 'b' 'c'}
NM = magic(30)
组合=([TX NM])
如何设法从中获取以下新结构:
COMBI = a b c在1,2,3列中,并且在第1:30行具有相同的值, NM的第一个数值从第4列开始,一直到第33列,并且具有第1:30行的值。
我总是得到,但我想要一个矩阵中的所有值
'a' 'b' 'c' 30x30 double
但我想要
1: 'a' 'b' 'c' 30 columns of double in cell notation, i.e. '3.54'
2: 'a' 'b' 'c' 30 columns of double in cell notation, i.e. '3.54'
3: 'a' 'b' 'c' 30 columns of double in cell notation, i.e. '3.54'
...
30: 'a' 'b' 'c' 30 columns of double in cell notation, i.e. '3.54'
我该怎么做?
更新1 :感谢Daniel,我用'更新了示例代码。 ' UPDATE 2 :首先构造一个空数组并将数字或文本数据粘贴到内部解决了问题,谢谢Benoit_11 更新3 :通过使用不等大小的数字矩阵,为Benoit提供了一个额外的剪辑:
%// To generalize;
n = numel(TX);
NumRowCol = size(NM,1);
NumCol = size(NM,2); % <- the 'new' part considers column size
%// Initialize the cell
MyCell = cell(NumRowCol,n+NumCol);
%// Insert text in first 3 columns
MyCell(:,1:3) = repmat(TX,NumRowCol,1);
%// Fill the rest with your numeric array in "cell" format.
MyCell(:,n+1:end)= num2cell(NM);
%// Display MyCell
MyCell;
答案 0 :(得分:2)
我想你想做类似以下的事情。我使用一个大小为5 x 5的数字数组来演示,但在你的情况下这个想法是相同的。
clear
clc
TX = {'a' 'b' 'c'};
NM = magic(5);
%// To generalize;
n = numel(TX);
NumRowCol = size(NM,1); %/// Since you use a magic matrix there are as many rows and columns. You might need to update this with different numeric arrays.
%// Initialize the cell
MyCell = cell(NumRowCol,n+NumRowCol);
%// Insert text in first 3 columns
MyCell(:,1:3) = repmat(TX,NumRowCol,1);
%// Fill the rest with your numeric array in "cell" format.
MyCell(:,n+1:end)= num2cell(NM);
%// Display MyCell
MyCell
MyCell =
'a' 'b' 'c' [17] [24] [ 1] [ 8] [15]
'a' 'b' 'c' [23] [ 5] [ 7] [14] [16]
'a' 'b' 'c' [ 4] [ 6] [13] [20] [22]
'a' 'b' 'c' [10] [12] [19] [21] [ 3]
'a' 'b' 'c' [11] [18] [25] [ 2] [ 9]