Azzera filtri
Azzera filtri

Create table in loop

224 visualizzazioni (ultimi 30 giorni)
Valeria Villegas-Medina
Valeria Villegas-Medina il 27 Ago 2020
Commentato: Adam Danz il 31 Ago 2020
Hello,
How can I create a table so that it grows with each iteration of a loop? I am trying to display all of the results from the loop but only manage to output the last.
Thank you for your time and help in advance!
idx = nchoosek(1:size(M), 3); %matrix of index trios
for k = 1:size(idx,1)
ix = idx(k ,:); % current idx trio
Lx = M(ix,1);
Ly = M(ix,2);
gap = M(ix,3);
L = sqrt((Lx.^2 + Ly.^2)/2);
lsq = lsqnonlin(@(phi) errFun(phi, L, gap), [1.5; 18; 1]);
a = lsq(1);
end
table(ix(:)', Lx', Ly', a)
  11 Commenti
Valeria Villegas-Medina
Valeria Villegas-Medina il 31 Ago 2020
I changed Lx, Ly, and a to be initialized with ones() instead but still get the error. Lsqnonlin needs to be called in the look I think becaue it needs to run for every iteration. Thanks for the debugging tutorial!
Adam Danz
Adam Danz il 31 Ago 2020
At a conceptual level, what's the reason behind fitting a nonlinear curve to matrices full of 1s (or 0s)? Anyway, the error is unrelated to the solutions in my answer so if you continue to have questions with lsqnonlin, I think the first step it to think through it at a conceptual level to figure out what you're supposed to be fitting.

Accedi per commentare.

Risposte (1)

Adam Danz
Adam Danz il 27 Ago 2020
Modificato: Adam Danz il 28 Ago 2020
1. Store the values within the loop and build the table later
Unless you're already working with table variables, it probably easier to store the values in each iteration and then build the table later. More importantly, this method is much faster and efficient than the other methods below (see last section on speed comparison).
In these demos, A and B are row vectors and C is a scalar.
rng('default') % for reproducibility
% Pre-allocate loop vars
A = nan(5,4);
B = nan(5,3);
C = nan(5,1);
% Create vars within loop
for i = 1:5
A(i,:) = rand(1,4);
B(i,:) = rand(1,3);
C(i) = rand(1);
end
% Build table
T = table(A,B,C,'VariableNames',{'BG','RO','GR'});
Result:
T =
5×3 table
BG RO GR
___________________________________________ _______________________________ ________
0.81472 0.90579 0.12699 0.91338 0.63236 0.09754 0.2785 0.54688
0.95751 0.96489 0.15761 0.97059 0.95717 0.48538 0.80028 0.14189
0.42176 0.91574 0.79221 0.95949 0.65574 0.035712 0.84913 0.93399
0.67874 0.75774 0.74313 0.39223 0.65548 0.17119 0.70605 0.031833
0.27692 0.046171 0.097132 0.82346 0.69483 0.3171 0.95022 0.034446
2. Build table within a loop - with preallocation
If you'd rather create the table within the loop and you know the size of the table and variable ahead of time, preallocate the table before the loop and then fill in each row and column using table indexing. Incidentally, preallocation does not save time (comparing this demo and the next one).
rng('default') % for reproducibility
T = table(zeros(5,4),zeros(5,3),zeros(5,1),'VariableNames', {'BG','RO','GR'});
for i = 1:5
T.BG(i,:) = rand(1,4);
T.RO(i,:) = rand(1,3);
T.GR(i) = rand(1);
end
Result:
T =
5×3 table
BG RO GR
___________________________________________ _______________________________ ________
0.81472 0.90579 0.12699 0.91338 0.63236 0.09754 0.2785 0.54688
0.95751 0.96489 0.15761 0.97059 0.95717 0.48538 0.80028 0.14189
0.42176 0.91574 0.79221 0.95949 0.65574 0.035712 0.84913 0.93399
0.67874 0.75774 0.74313 0.39223 0.65548 0.17119 0.70605 0.031833
0.27692 0.046171 0.097132 0.82346 0.69483 0.3171 0.95022 0.034446
3. Build table within a loop - without preallocation
If you don't know the size of the table and variables ahead of time, you can build the table within the loop by vertical concatenation. The sizes within each variable must still be consistent, though.
rng('default') % for reproducibility
T = table();
tempTable = table();
for i = 1:5
tempTable.BG = rand(1,4);
tempTable.RO = rand(1,3);
tempTable.GR = rand(1);
T = [T;tempTable];
end
Result:
T =
5×3 table
BG RO GR
___________________________________________ _______________________________ ________
0.81472 0.90579 0.12699 0.91338 0.63236 0.09754 0.2785 0.54688
0.95751 0.96489 0.15761 0.97059 0.95717 0.48538 0.80028 0.14189
0.42176 0.91574 0.79221 0.95949 0.65574 0.035712 0.84913 0.93399
0.67874 0.75774 0.74313 0.39223 0.65548 0.17119 0.70605 0.031833
0.27692 0.046171 0.097132 0.82346 0.69483 0.3171 0.95022 0.034446
Efficiency and speed
The fastest and most efficient method in this answer is the first one, by far!
Each method was timed 500 times using tic/toc and the median durations were compared.
  • 1 was 174 times faster than 2
  • 1 was 142 times faster than 3
  • 3 was 1.15 times faster than 2 (I checked this twice, #3 was faster than #2 by factors of 1.1 and 1.2)
I haven't looked into my table preallocation is slower than without it but others have reported this as well (example).

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by