logical operation on matrix

1 view (last 30 days)
Faisal Al-Wazir
Faisal Al-Wazir on 8 Mar 2022
Commented: Image Analyst on 8 Mar 2022
(Return logical 0 or 1) if matrix A contains ten values between 500 and 600.
A=[1 2 3;4 5 6; 4 5 2]
A = 3×3
1 2 3 4 5 6 4 5 2
A(A>[1:2:6])=true %SMALL NUMBERS FOR EXAMPLE
A = 3×3
1 2 3 1 1 1 1 1 2

Accepted Answer

DGM
DGM on 8 Mar 2022
I'm going to assume that the text question is what you're after:
A = randi([100 999],10,10) % a bunch of integers
A = 10×10
756 350 720 933 311 504 214 315 736 279 334 959 539 277 582 701 528 418 941 523 768 367 470 157 524 543 177 131 833 416 995 572 341 728 828 280 875 161 250 482 201 593 316 296 813 148 341 356 435 870 392 162 693 700 398 301 509 438 390 533 284 593 774 286 710 781 976 337 833 527 616 751 880 961 804 339 626 177 588 956 946 771 904 346 665 560 106 544 366 708 320 446 804 673 429 882 836 850 224 371
mk = A>=500 & A<=600 %find locations in the specified range
mk = 10×10 logical array
0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 1 0 0 1 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0
has10inrange = nnz(mk)>=10 % are there at least 10 values in the range?
has10inrange = logical
1
  1 Comment
Image Analyst
Image Analyst on 8 Mar 2022
Or if you want to know if "A contains ten values between 500 and 600"
has10inRange = nnz(mk) == 10 % has10inRange is true if there are exactly 10 values in the range.

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!