Finding the maximum value in a single

12 views (last 30 days)
I have a 1X128X3 single. I want to find the maximum values in each of the 3 channels, how can this be accomplished?

Accepted Answer

John D'Errico
John D'Errico on 22 Jan 2023
Edited: John D'Errico on 22 Jan 2023
The max function did not work? Why not? Surely you tried it?
S = rand([1,128,3],'single')
S = 1×128×3 single array
S(:,:,1) = 0.9557 0.1427 0.3405 0.6756 0.9683 0.2260 0.2643 0.8766 0.1601 0.3533 0.1217 0.5791 0.1570 0.9396 0.3561 0.3374 0.2021 0.9924 0.1874 0.1094 0.4630 0.4364 0.3554 0.9370 0.0987 0.4981 0.1302 0.0768 0.9896 0.2384 0.1609 0.2545 0.5572 0.7844 0.8800 0.6078 0.1950 0.0075 0.5522 0.8453 0.3545 0.2669 0.8706 0.4797 0.9956 0.5484 0.8204 0.8800 0.3609 0.3253 0.5001 0.0510 0.7105 0.7387 0.3200 0.9856 0.8967 0.8876 0.0119 0.7305 0.6968 0.1452 0.9469 0.0811 0.7298 0.2679 0.5191 0.2916 0.8015 0.1417 0.0169 0.7617 0.3939 0.5777 0.0981 0.5780 0.8458 0.6728 0.6075 0.0170 0.4806 0.8411 0.5548 0.5855 0.0772 0.3482 0.1833 0.3835 0.7599 0.8728 0.3124 0.5976 0.1430 0.3458 0.6698 0.9066 0.1878 0.8884 0.8400 0.3497 0.1501 0.7870 0.3385 0.6183 0.9170 0.6736 0.6312 0.3466 0.2678 0.5486 0.7385 0.8791 0.3033 0.4010 0.8053 0.1195 0.7835 0.9073 0.9074 0.5491 0.4376 0.5875 0.8007 0.5016 0.3179 0.8934 0.3880 0.8169 S(:,:,2) = 0.6750 0.0244 0.7297 0.7161 0.7345 0.3474 0.3519 0.9932 0.0022 0.5864 0.3253 0.5362 0.7495 0.2942 0.1847 0.4343 0.0366 0.6893 0.2565 0.9985 0.1643 0.4219 0.7648 0.7371 0.2732 0.6387 0.2065 0.8124 0.1072 0.3629 0.9866 0.8684 0.6886 0.7622 0.5557 0.5917 0.0315 0.2410 0.6593 0.9543 0.7830 0.0649 0.7360 0.6809 0.0108 0.3909 0.5070 0.6011 0.7611 0.3374 0.3473 0.1922 0.8107 0.5036 0.1575 0.3710 0.4097 0.7914 0.5949 0.7992 0.6088 0.0735 0.7162 0.6951 0.6174 0.0584 0.1226 0.9477 0.2845 0.3434 0.7580 0.7480 0.7757 0.7594 0.5944 0.2847 0.4666 0.2775 0.4339 0.0988 0.6698 0.0340 0.6814 0.6940 0.9222 0.1348 0.2211 0.8204 0.3004 0.6479 0.4911 0.9741 0.9958 0.2539 0.9974 0.3831 0.5335 0.0473 0.9102 0.7727 0.5120 0.8768 0.2086 0.8226 0.4095 0.4167 0.7353 0.1733 0.8169 0.5070 0.2328 0.2235 0.4618 0.0158 0.1165 0.4908 0.7861 0.4782 0.6259 0.3511 0.7469 0.7876 0.7971 0.5446 0.2603 0.1840 0.6295 0.9905 S(:,:,3) = 0.4791 0.2940 0.6249 0.2390 0.0943 0.6676 0.3252 0.6926 0.2384 0.1067 0.8984 0.4460 0.0757 0.2423 0.4307 0.2851 0.2647 0.1595 0.0255 0.9026 0.6795 0.9780 0.7197 0.1265 0.1647 0.7993 0.6764 0.8225 0.9037 0.2253 0.2844 0.4565 0.2419 0.0760 0.8307 0.6283 0.8663 0.2074 0.8725 0.5262 0.3077 0.7511 0.6912 0.9238 0.1845 0.0062 0.9845 0.0728 0.5196 0.6840 0.5730 0.9710 0.1344 0.8643 0.8640 0.6774 0.0758 0.9000 0.1705 0.0219 0.8878 0.3826 0.9206 0.2563 0.6831 0.7040 0.0375 0.0802 0.7031 0.1078 0.7203 0.7382 0.4817 0.8284 0.8753 0.5169 0.1553 0.8930 0.8759 0.9566 0.7253 0.1547 0.8878 0.7939 0.9599 0.1355 0.5782 0.1458 0.3618 0.6818 0.3221 0.1438 0.7235 0.8641 0.4978 0.1295 0.8315 0.2729 0.4741 0.1573 0.9220 0.3514 0.3494 0.2594 0.7933 0.1565 0.6889 0.9444 0.4240 0.5562 0.2459 0.2756 0.4601 0.2840 0.9458 0.4839 0.6426 0.0580 0.7558 0.0852 0.7317 0.1730 0.2258 0.4086 0.4143 0.8189 0.0147 0.2181
Smax = max(S,[],2)
Smax = 1×1×3 single array
Smax(:,:,1) = 0.9956 Smax(:,:,2) = 0.9985 Smax(:,:,3) = 0.9845
If you don't like that the result is now 1x1x3, use squeeze, or reshape to put it in the shape you prefer.
squeeze(Smax)
ans = 3×1
0.9956 0.9985 0.9845
Actually, a simple max(S) would also have worked, since max will apply to the first non-singleton dimension.
max(S)
ans = 1×1×3 single array
ans(:,:,1) = 0.9956 ans(:,:,2) = 0.9985 ans(:,:,3) = 0.9845
So all you had to do was try the max function.

More Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 22 Jan 2023
Edited: Sulaymon Eshkabilov on 22 Jan 2023
Here is how you can get max values from 1-by-n-by-3 array from each channel exaplined with a simple example:
PP(:,:,1) = round(10*randn(1,128), 2);
PP(:,:,2) = round(10*randn(1,128), 2);
PP(:,:,3) = round(10*randn(1,128), 2);
[Values, Index]=max(PP)
Values =
Values(:,:,1) = 25.7400 Values(:,:,2) = 22.0600 Values(:,:,3) = 26.9900
Index =
Index(:,:,1) = 56 Index(:,:,2) = 30 Index(:,:,3) = 93
% If the array is m-by-n-by-3:
P(:,:,1) = round(10*randn(20, 30), 2);
P(:,:,2) = round(10*randn(20, 30), 2);
P(:,:,3) = round(10*randn(20, 30), 2);
[Values, Index]=max(max(P))
Values =
Values(:,:,1) = 35.3400 Values(:,:,2) = 30.0800 Values(:,:,3) = 28.7700
Index =
Index(:,:,1) = 15 Index(:,:,2) = 22 Index(:,:,3) = 6

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!