I think I figured out the problem. I think I don't need eval command at all to do the job. and eval was causing the problem. but still, I would appreciate if you give comments or alternate solution.
how can i interpolate a 2d matrix in to a 3d array?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi everyone
I have arrays as follow:
cros34(2951,201) and wl(2951,1) in which every element in each column of cros34 is related to one element of wl array. now I want to interpolate cros34 matrix into J(67,201,16) array in which elements of J array is related to wc(67,1) array, in a similar way that elements of cros34 array is related to wl array. note that all elements of wl array are in the interval of two elements in wc array. and the third dimension of J array is related to pressure(16,1) array.
i have a matlab script as below:
rxns = {'J'};
eval([rxns '= zeros(67,201,16);'])
for t=1:201
eval([rxns '(:,t,1) = interp1(wll,cros34(:,t),wc);'])
end
for p=1:16
eval([rxns '(:,:,p) = ' rxns '(:,:,1);'])
end
eval([rxns '(isnan(' rxns ')) = 0;'])
eval(['ncwrite(''temp_prs_GT200nm_JPL10_c140303_OCS.nc'',''' rxns ''',' rxns ')'])
but it gives me error as below:
Undefined function 'eval' for input arguments of type 'cell'.
note that finally, i'm writing J array to an NC file.
so I was wondering what's wrong with my script or is there any other way to do this. I uploaded the .mat files of arrays in case if it's required
thank you in advance
2 Kommentare
Stephen23
am 27 Feb. 2018
Bearbeitet: Stephen23
am 27 Feb. 2018
"I was wondering what's wrong with my script "
The design decision to use eval for every single operation.
When code is written normally without eval the MATLAB code analyzer gives code hints and points out syntax errors, syntax warnings, or tips for improvements in the code. This would have helped you without you even having to do anything apart from paying attention to the messages that the editor would show you. When beginners use eval they pick a method of writing code that removes all automatic code checking, and at the same time makes it harder for them to debug their code.
Using eval also makes code slower, because every line must be executed again on every loop iteration, which means that the MATLAB JIT code optimizer cannot accelerate the code. So that design decision also forced you to write slow code.
And then you got stuck with a bug that made it much harder to even identify, because of the obfuscation that eval results in. That is what eval does, so no one here will be very surprised at that.
"I think I don't need eval command at all to do the job. and eval was causing the problem."
Get rid of the eval entirely. Not only do you not need it, it is actually making your code much slower, more complex, buggier, and harder to debug. Read this to know why:
Akzeptierte Antwort
Guillaume
am 24 Feb. 2018
Bearbeitet: Guillaume
am 27 Feb. 2018
You never need eval. It's as simple as that. The cases where you'd have to use eval are very rare and unless you're doing some very advanced programming and know matlab inside out, you don't need eval.
I see absolutely no justification or need for eval in any of the lines of code you've written. All it does it makes the code more complicated, more difficult to read, near impossible to debug, and disables the syntax checker, tab completion and all the useful tools matlab's editor provides.
Your code can be rewritten as
J = zeros(67,201);
for t=1:201
J(:,t) = interp1(wll,cros34(:,t),wc);
end
J = repmat(J, [1, 1, 16]);
J(isnan(J)) = 0;
ncwrite('temp_prs_GT200nm_JPL10_c140303_OCS.nc','J', J);
edit: Apparently, in older versions of matlab, repmat only accepted a single vector for the replication array
6 Kommentare
Weitere Antworten (1)
Walter Roberson
am 26 Feb. 2018
R2013a (the one you are using) was the last release which did not permit an indefinite number of arguments to repmat. In R2013a, you could use repmat(A,m,n) or you could use repmat(A,VECTOR) where VECTOR was a vector with at least two entries.
4 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices 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!