matlab-创建零行和一行索引的矩阵
我有一个向量matlab-创建零行和一行索引的矩阵,matlab,matrix,Matlab,Matrix,我有一个向量a=[1;6;8] 并希望创建一个包含n列和大小(a,1)行的矩阵 每一行都是零,但是a(i)索引上是一 >使矩阵(a,10) 使用sparse numCol = 10; % number of colums in output matrix, should not be greater than max(a) mat = sparse( 1:numel(a), a, 1, numel(a), numCol ); 如果你想要一个完整的矩阵,就使用 full(mat) 我的第
a=[1;6;8]
并希望创建一个包含n
列和大小(a,1)
行的矩阵
每一行都是零,但是a(i)
索引上是一
>使矩阵(a,10)
使用
sparse
numCol = 10; % number of colums in output matrix, should not be greater than max(a)
mat = sparse( 1:numel(a), a, 1, numel(a), numCol );
如果你想要一个完整的矩阵,就使用
full(mat)
我的第一个想法是:
a = [1;6;8];
nCols = 10;
nRows = length(a);
M = zeros(nRows,nCols);
M(:,a) = eye(nRows)
基本上,眼睛被分配到矩阵的右列
a = [1;6;8];
nCols = 10;
nRows = length(a);
M = zeros(nRows,nCols);
M(:,a) = eye(nRows)