how to vectrorized this code?

1 次查看(过去 30 天)
close all
clear all
clc
tic;
i=1;
for k=1:1:10;
for a=1:1:10;
nump(i,:) =k*a;
denp(i,:) =[1 a 0];
i=i+1;
end
end

采纳的回答

Jan
Jan 2017-10-9
编辑:Jan 2017-10-9
nump = (1:10) .* (1:10).'; % Auto-Expand >= Matlab R2016b
For older Matlab versions:
nump = bsxfun(@times, (1:10), (1:10).');
denp from KL's solution is perfect already. This is 2 microseconds(!) faster on my computer, but less nice:
denp = zeros(100, 3);
denp(:, 1) = 1;
denp(:, 2) = repmat((1:10)', 10, 1);

更多回答(1 个)

KL
KL 2017-10-9
nump = cell2mat(arrayfun(@(x) x:x:x*10,1:10,'uni',0))';
denp = [ones(100,1) repmat((1:10)',10,1), zeros(100,1)];

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by