MATLAB Answers

how can i interpolate a 2d matrix in to a 3d array?

조회 수: 4(최근 30일)
Joseph
Joseph 23 Feb 2018
Commented: Joseph 1 Mar 2018
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

Joseph
Joseph 24 Feb 2018
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.
Stephen Cobeldick
Stephen Cobeldick 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:

로그인 to comment.

채택된 답변

Guillaume
Guillaume 24 Feb 2018
Guillaume 님이 편집함. 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

표시 이전 댓글 수: 3
Walter Roberson
Walter Roberson 26 Feb 2018
If you run the code then when it stops with the error, check what shows up for
which repmat(J)
Joseph
Joseph 26 Feb 2018
Hi,
it shows the below:
which repmat(J)
C:\Program Files\MATLAB\R2013a\toolbox\matlab\elmat\repmat.m
Guillaume
Guillaume 27 Feb 2018
Fixed my answer so that it works in R2013a.

로그인 to comment.

More Answers (1)

Walter Roberson
Walter Roberson 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

표시 이전 댓글 수: 1
Joseph
Joseph 1 Mar 2018
Dear Guillaume,
I apologize, I didn't mean to offend anyone. I'm fairly new to Matlab and didn't know that I can only accept one answer to the question. I was waiting to debug your answer so I can accept it. since I already accepted Mr. Roberson's answer, with his permission I can reaccept your answer.
My apologies again and thank you for answering my question. i really do appreciate it.
Joseph
Joseph 1 Mar 2018
Thank you, Walter Roberson.

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by