defining upper and lower limits of a matrix
Show older comments
Hello all I have a smaller question and a larger question.
Small question: In the attached 'viscosity_test.mat' file is there a way to set the minimum value to 1e19? Is the process similar if I decide I want to set a maximum value?
Larger question: I am running a finite difference numerical model on an uploaded .png file (shown below). I believe the shape boundary is causing artificially low and high values to be output. Is there a way to troubleshoot this that doesn't involve going through and manipulating the minimum and maximum values after the model runs? I can provide more details if that is unclear.

Thank you for any help!
Accepted Answer
More Answers (3)
John D'Errico
on 28 Jan 2025
Edited: John D'Errico
on 28 Jan 2025
1 vote
Is there any way to set the min and/or maximum values of the elements in a matrix, and do so automatically? NO. Well, not without writing a fully custom class for matrix maipulations, and that would force you to write all code to use those matrices, everything from arithmetic to indexing, etc. And it would probably be godawfully slow, at least compared to what you use normally.
It is of course trivially simple to alter the results after the fact. But that does nothing of any real value, and effectively just pretends the problem does not exist. If there is an issue with your code and the numerical analysis, you need to fix the problem, not just hide the problem by hiding the symptoms.
Your second question is highly unclear. Is there any way to troubleshoot it, without fully understanding the code, and what you wrote? Of course not. You will probably need to learn to use the debugging tools. Watching for when and where the elements grow. Figure out why. Does that necessarily involve manipulating the min and max values, AFTER the model runs? I see no reason why it should. If you want to understand what is happening, and why, you need to look carefully into your code.
If you were using release R2024a or later (or if upgrading is an option) you could use the clip function.
M = magic(5)
M2 = clip(M, 7, 18)
Walter Roberson
on 28 Jan 2025
The minimum and maximum value of a matrix are what they are. The best you can do is to create a new matrix with the desired characteristics.
B = max(1e19, A) %sets the lowest value to be 1e19, does not change anything more
C = min(1e28, B) %sets the highest value to be 1e28, does not change anything more
D = rescale(A, 1e19, 1e28)
%does linear interpolation between the actual lowest value in A and the
%actual highest value in A, mapping them to 1e19 to 1e28. All values in A
%are affected
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
