Is it possible to define constant value in a matrix ?

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 Comments

We need more context. Can you explain what you are doing in more detail? If you mean you have a matrix of values and you want to fix one element but let the others change, then the answer is no for all of the numeric and logical and char types. You could invent your own custom class for this, but that would be a lot of work. Thus, if you explained in more detail what you are doing perhaps we could suggest other ways of doing things.
Indeed, I would like to fix one element and let the others change.
In fact, I have a big matrix and there are conditions due to the shape and boundary limits that must remain the same.
Then I am iterating over the hole matrix, but i would like that the conditions are fixed and don't change with the iteration
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.
Yeah, indeed. Thank you for your answers.

Sign in to comment.

 Accepted Answer

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 Comment

I think that it's a lot of work for what I'm tring to do! Thank you for your answer.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!