Is it possible to define constant value in a matrix ?

23 vues (au cours des 30 derniers jours)
Quentin Dragomir
Quentin Dragomir le 23 Oct 2018
Hello,
Is it possible to have constant values in a matrix?
I mean, there are some places where the value can't change.
The problem is that I'm changing the hole matrix.
So I wonder if there is a command like:
const global pi = 3.14;
const double pi = 3.14;
Thank you
  4 commentaires
James Tursa
James Tursa le 23 Oct 2018
You will have to manually set these elements back to their desired states every time your code touches the matrix. If that is not feasible because something else besides your code is changing it, then you will have to find another way to solve your problem.
Quentin Dragomir
Quentin Dragomir le 23 Oct 2018
Yeah, indeed. Thank you for your answers.

Connectez-vous pour commenter.

Réponse acceptée

John D'Errico
John D'Errico le 23 Oct 2018
You cannot enforce the requirement that any specific element of a matrix will never be changed (or fix some matrix as a whole to never change), any more than you can make up some syntax as you wish, and hope that it would apply. So, no, you cannot tell MATLAB that any given matrix (or matrix element) cannot be touched. There is no flag you can set, etc.
To some extent however, there are things you might be able to do. For example, you can create your own personal matrix class. In there, you could define indexing in such a way that you could prevent specific elements of the custom matrix class from changing. Of course, that would not stop someone from simply clearing the variable, replacing it as they wish.
Would the above custom matrix class be of any real value? Probably not. To use it in any way, you would need to write a complete set of operations. Absolutely anything you wanted to do with it would then require custom code. Even something as simple as a matrix multiply? Yep. You would need to write all the loops yourself. And it would be slow as molasses at the South pole, since you would not be able to then use the highly optimized matrix multiplication tools already in existence. The blas would not be employed, etc. If you wanted to use chol or mldivide, then you would need to write the complete code yourself.
So I'm sorry, but you cannot do what you want, at least not in any realistic way.
  1 commentaire
Quentin Dragomir
Quentin Dragomir le 23 Oct 2018
I think that it's a lot of work for what I'm tring to do! Thank you for your answer.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by