how can i interpolate a 2d matrix in to a 3d array?
Show older comments
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 Comments
Joseph
on 24 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:
Accepted Answer
More Answers (1)
Walter Roberson
on 26 Feb 2018
1 vote
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 Comments
It's kind of ironic that the accepted answer is an answer completely unrelated to the question!
Note that the minor technical issue with repmat has nothing to do with the crux of the problem which is the usage of eval that was completely unnecessary.
Joseph
on 1 Mar 2018
Walter Roberson
on 1 Mar 2018
Go ahead.
Joseph
on 1 Mar 2018
Categories
Find more on Matrix Indexing 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!