Replace 'eval' for more efficient code
2 次查看(过去 30 天)
显示 更早的评论
I'm trying to simulate some results by conditioning on different sets of variables i.e. the power set of the covariates. In order to label the output, I'm using 'eval' depending on the conditioning set, but this is extremely slow. Is there a better way to do this? I have attached the code.
0 个评论
采纳的回答
更多回答(1 个)
the cyclist
2015-9-5
I did not dive deeply into your code, but one thing you are doing (which is common when people resort to using eval) is naming variables pval_1_2, etc.
You can avoid that need by using cell arrays to store variables. For example
pval{1,2} = rand(2,5);
pval(6,7) = rand(3,4);
etc
Here is one of many places on this forum that discusses the problem of naming variables A1, A2, etc.
1 个评论
Image Analyst
2015-9-5
Further explanation in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!