defining upper and lower limits of a matrix

5 views (last 30 days)
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

Voss
Voss on 28 Jan 2025
Edited: Voss on 28 Jan 2025
Load and visualize data
etan = load('viscosity_test.mat').etan;
% check the min and max value of etan
[etan_min,etan_max] = bounds(etan,'all')
etan_min = 0
etan_max = 3.7574e+22
% make a histogram of log10(etan) for reference
figure()
histogram(log10(etan(:)))
Set min and max values
% set any element of etan that's less than 1e19 (or NaN) to 1e19
etan = max(etan,1e19);
% set any element of etan that's greater than 3e21 (or NaN) to 3e21
etan = min(etan,3e21);
Visualize new data
% check the min and max value of etan again
[etan_min,etan_max] = bounds(etan,'all')
etan_min = 1.0000e+19
etan_max = 3.0000e+21
% make a histogram of the new log10(etan) for reference
figure()
histogram(log10(etan(:)))

More Answers (3)

John D'Errico
John D'Errico on 28 Jan 2025
Edited: John D'Errico on 28 Jan 2025
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.

Steven Lord
Steven Lord on 29 Jan 2025
If you were using release R2024a or later (or if upgrading is an option) you could use the clip function.
M = magic(5)
M = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
M2 = clip(M, 7, 18)
M2 = 5×5
17 18 7 8 15 18 7 7 14 16 7 7 13 18 18 10 12 18 18 7 11 18 18 7 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Walter Roberson
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

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!