Saving existing matrix

1 次查看(过去 30 天)
Nicolas
Nicolas 2011-4-19
Hi,
I would like to find a way to tell my code that if at the end of the loop the matrix 'P' exists then to save it, if not to continue the loop. I wrote that:
z=exist('P','var')
if z>0
save
end
It is wroking but, is it a good way to write it?
Cheers

采纳的回答

Matt Fig
Matt Fig 2011-4-19
Why would P exist sometimes and not others? If it is created somewhere in the loop, presumably an IF statement or other such branch, why not just save it there? Since your saved variable will be overwritten each time through the loop (that P exists), why not save after the loop?
  1 个评论
Nicolas
Nicolas 2011-4-19
basically, I'm studying a lot of data with the same range of parameters (i have 3 nested loops). For some data and some parameters P doesn't exist, while for the same data and different parameters P exists.
I'm saving P outside the loop (as you mentionned) and each time it is saved like P-1-1, then P-1-2 and so on.
basically I'm trying to make my matlab writting a bit better, and i try to avoid "if" or "for" when not necessary, to make my code more efficient.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by