MATLAB: How to vectorize these “for loop”

control systemMATLAB

clc;
clear all;
close all;
i=1;
for k=1:1:10;
for a=1:1:10;
for b= 1:1:10;
for c=1:1:10;
nump(i,:)=[k a*k];
denmp(i,:)=[1 b+c b*c];
i=i+1;
end
end
end
end

Best Answer

If you want to vectorize your problem, you can write something like this:
n=10;
m=n*n;
%

A=repmat(1:n,n*m,1);
B=repmat(1:n,m,n) ;
C=repmat((1:n),n*m,1);
nump=[A(:) B(:).*C(:)];
%
B=repmat(1:n,n,1)+repmat((1:n)',1,n);
C=repmat(1:n,n,1).*repmat((1:n)',1,n);
denmp=[ones(m*m,1) repmat(B(:),m,1) repmat(C(:),m,1)];
Maybe we can make it even faster...
Fabien Baillon.