Creating table from Workspace variables within a for loop
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello Everyone,
I am rather new to Matlab and right now are running slightly into a wall. I want to fit data from a 3D matrix and extract the fit parameters into tables, i.e. my parameters are a, b, c, d, e etc. and I basically want for each x-y value of my matrix a new matrix with the respective fit parameter for that x-y coordinate. I got so far to get the fitting working, when I write the a from my first loop, that obviously works, but in the second loop the a value of that loop just replaces my a from the first loop, and so on.
The Matlab version I work with is R2014a.
for i=1:512
for j=1:640
y2=A(i,j,:);
y1=y2(:,2:nFiles);
y=transpose(y1);
x1=2 : nFiles;
x=transpose(x1);
try
F = fit(x,y,'a*exp(-x/b)+c*exp(-x/d)+g','Algorithm', 'Levenberg-Marquardt','Robust', 'Bisquare' ,'MaxIter', 50, 'StartPoint', [40 0.5 1 75 0]);
plot(F,x,y);
R = coeffvalues(F);
ParamA = table(R(1:1));
catch ME
end
end
end
1 Kommentar
Guillaume
am 3 Feb. 2020
Why the try...catch? Which line do you expect to fail?
This bit:
y2=A(i,j,:);
y1=y2(:,2:nFiles);
y=transpose(y1);
x1=2 : nFiles;
x=transpose(x1);
is very confusing. Avoid renaming variables. Your A is changed into a y2 which then becomes a y1 (why are we going back in indices?), which then becomes a y. Then we have a x1 that becomes a x. Stick to one variable. On top of that A appears to be a 3D matrix, which is then indexed to produce a 1x1xN matrix. On the next line you ask for all the rows with the : (there is just one!), and column 2:nFiles (there's just one!) and don't index the 3rd dimension (which is where all the elements are now). Now, if you don't index all the dimensions they get lumped with the last dimension indexed, but chances are you didn't know that and your indexing worked by luck. Overall:
y = reshape(A(i, j, 2:end), [], 1); %extract page 2:end at row i, column j,and reshape into column.
would achieve the same and be clearer.
It's unclear what you're trying to achieve with table(A(1:1). Note that A(1:1) is the same as A(1) (or A(1,1,1))
Akzeptierte Antwort
Guillaume
am 3 Feb. 2020
Bearbeitet: Guillaume
am 4 Feb. 2020
See my comments to the question. Writing code that is easily understood is just as critical as writing code that works!
Note that you're performing 512 x 640 fit. This is going to take a while.
I'm assuming that in your original code, size(A, 1) is 512, size(A, 2) is 640 and size(A, 3) is nFiles. Avoid using hardcoded constants which need changing if you change the size of arrays. Simply tell matlab to use the size of the arrays, whatever that is.
x = (2:size(A, 3)).'; %no point wasting time doing this repeatedly inside a loop.
numvar = 5; %number of variables in the fit function
[rows, columns] = ndgrid(1:size(A, 1), 1:size(A, 2)); %create pairs of rows/columns to store in output table and to index over
fitresults = zeros(numel(rows), numvar); %preallocate matrix to store fit results
for step = 1:numel(rows)
y = reshape(A(rows(step), columns(step), 2:end), [], 1); %extract 3rd dimension and convert to column vector
try %fit function may error
F = fit(x,y,'a*exp(-x/b)+c*exp(-x/d)+g','Algorithm', 'Levenberg-Marquardt','Robust', 'Bisquare' ,'MaxIter', 50, 'StartPoint', [40 0.5 1 75 0]);
fitresults(step, :) = coeffvalues(F);
catch
fitresults(step, :) = NaN; %if failure store NaN
end
results = array2table([rows(:), columns(:), fitresults], 'VariableNames', [{'row'; 'column'}; coeffnames(F)])
edit: fixed bug in ndgrid call and added error handling in case fit fails
3 Kommentare
Guillaume
am 4 Feb. 2020
Yes, I made a mistake in the ndgrid call. Fixed now. I've added the try...catch with a comment explaining why it's there. Note that an essential part of writing good code is writing comments.
I would recommend you test the code on a much smaller A to start with, at least with less rows and columns (size of 3rd dimension doesn't matter). It's going to take a long time to perform over 32,000 fits.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!