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

3 ビュー (過去 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 Center および File ExchangeText Data Preparation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by