Nested loop - storing vectors into matrix

2 views (last 30 days)
Hello,
I would like to store each vector from a loop a matrix (27 rows).
The current code creates the 27 vectors but overwrites previous vectors. How I can save the vector from each run into a matrix? The file exchange demo solution does not work.
%% ONLINE VERSION
clear all
% Random numbers in vector
r = random('Uniform',1,10,1,10);
u = sort(r) % here redundant
% Fixed parameters
k = 1;
l = 1;
m = 1;
% Creating a matrix based on vectors for each parameter change
a = [0 1 2];
b = [0 1 2];
c = [0 1 2];
% Vector with parameter a_i, b_i, c_i
% Creating loop
for i=1:length(a)
for j=1:length(b)
for f=1:length(c)
xDL = k + a(i) + (l + b(j))*u*(m + c(f))
lxDL = [xDL a(i) b(j) c(f)]
end
end
end
Thanks!
  2 Comments
Rick Rosson
Rick Rosson on 15 Jul 2011
Please format your code by inserting two blank spaces at the beginning of each line of code. Thanks!

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 16 Jul 2011
Before the loop, set
lxDL = [];
Change
lxDL = [xDL a(i) b(j) c(f)]
to
lxDL = [lxDL; xDl a(i) b(j) c(f)];
  1 Comment
Walter Roberson
Walter Roberson on 16 Jul 2011
Note: this will be pretty inefficient in general. Much more efficient for computation (but not for memory) would be
[A B C] = ndgrid(a,b,c);
lxDL = k + A(:) + (l + B(:)).*u.*(m + C(:));
lxDL(:,2:4) = [A(:), B(:), C(:)];

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!