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

조회 수: 1 (최근 30일)
Joseph
Joseph 2018년 2월 23일
댓글: Joseph 2018년 3월 1일
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 2018년 2월 24일
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.
Stephen23
Stephen23 2018년 2월 27일
편집: Stephen23 2018년 2월 27일
"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:

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2018년 2월 24일
편집: Guillaume 2018년 2월 27일
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
Joseph
Joseph 2018년 2월 26일
Hi,
it shows the below:
which repmat(J)
C:\Program Files\MATLAB\R2013a\toolbox\matlab\elmat\repmat.m
Guillaume
Guillaume 2018년 2월 27일
Fixed my answer so that it works in R2013a.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 2월 26일
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
Walter Roberson
Walter Roberson 2018년 3월 1일
Go ahead.
Joseph
Joseph 2018년 3월 1일
Thank you, Walter Roberson.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by