Expand 1-D array to 2-D matrix using 'ones' function and colon ':'
6 ビュー (過去 30 日間)
古いコメントを表示
Example:
matrixA = 6:10;
matrixA = matrixA(ones(1,5), :);
or:
matrixB = (6:10)';
matrixB = matrixB(:, ones(1,5));
My Question:
What's going on here? I just don't get it. Is there any reference on this "exotic" usage? Many thanks for any inputs!
0 件のコメント
採用された回答
Daniel Shub
2011 年 9 月 5 日
It is not that crazy. It is basically
matrixA = 6:10;
for ii = 1:5
for jj = 1:5
temp(ii, jj) = matrixA(1, jj);
end
end
matrixA = temp;
but lump together into a vectorized version. You can get rid of the inner loop with:
matrixA = 6:10;
for ii = 1:5
temp(ii, :) = matrixA(1, :);
end
matrixA = temp;
and you can finally get rid of all loops with
matrixA = 6:10;
temp = matrixA(ones(1,5), :);
matrixA = temp;
0 件のコメント
その他の回答 (2 件)
Oleg Komarov
2011 年 9 月 5 日
matrixA = matrixA(ones(1,5), :)
select all elements from row one 5 times.
matrixB = matrixB(:, ones(1,5));
select all elements from column one 5 times.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!