Clear Filters
Clear Filters

How to draw this figure?

1 view (last 30 days)
z cy
z cy on 8 May 2022
Commented: Image Analyst on 8 May 2022
I have a matrix A 16*16, and an index B. I want to make the pixel of index B red as shown in Figure. It's obvious that we can easily obtan grey image through imshow(A), but how to add red pixels?
A = [-0.999317947480371 -0.993536503585372 -0.969862437773368 -0.906832750196398 -0.759705716591495 -0.481332916950500 -0.153677095108110 0.0141875446062520 -0.0685948868128668 -0.328929988989340 -0.629150891625282 -0.852076097628516 -0.959837865788046 -0.992595983390425 -0.999059474048030 -0.999933713928324;
-0.998496616532260 -0.985598854655850 -0.929901994546247 -0.781710068115769 -0.482597092641038 -0.0362238815467362 0.371217526815227 0.543090608028069 0.478132838616072 0.234246392744947 -0.153302453089086 -0.573278284411362 -0.851351393695826 -0.963688227796016 -0.993762279329382 -0.999337043346916;
-0.995434490728516 -0.967620311535804 -0.863806160702648 -0.612236686974160 -0.186999353246921 0.280766626822551 0.552541331194880 0.586098280009986 0.531486858619764 0.454018934575730 0.230044409279903 -0.204581543975927 -0.639180539482132 -0.885420014372121 -0.974348251331413 -0.996236888546869;
-0.987160499189578 -0.931427299443134 -0.759716812156651 -0.394482792309088 0.108182160202820 0.471188949630731 0.503480340709777 0.335586083931315 0.232335876665255 0.304579043289162 0.358074579227322 0.115659673513168 -0.355231483588663 -0.740679097390359 -0.926078406847594 -0.985677343789221;
-0.971586928949938 -0.872458213033895 -0.610309028929792 -0.132902238738726 0.366390418664472 0.540622560526311 0.325005512271312 -0.0119505594362531 -0.179342191067915 -0.0420368559588870 0.248681795512451 0.297403754620354 -0.0632610056260224 -0.539113882327251 -0.839789031978881 -0.961837090618874;
-0.948091962614030 -0.788313118639847 -0.421881681579890 0.133190346858710 0.538287507930558 0.476064318743290 0.0544066504400661 -0.363909862720283 -0.549869745479283 -0.409558846145971 0.00674225711486494 0.329568725914733 0.165586215561823 -0.323035217203989 -0.726225360511520 -0.925757449921316;
-0.914187288441434 -0.682056788004283 -0.218598709988698 0.352872368633207 0.602817009933680 0.302561572259300 -0.249242626273231 -0.652008419402765 -0.798362057647752 -0.678856137839372 -0.248005715458174 0.262006508209699 0.308918778809463 -0.136236575399137 -0.610968817896169 -0.884121384015371;
-0.872245286353229 -0.571318177056808 -0.0376154150892203 0.502736963155606 0.574275936387609 0.0714784092791476 -0.519492132855641 -0.836145331889220 -0.916248993374835 -0.815080748895835 -0.419315533022597 0.185009018329336 0.393747530022390 0.00623507503954278 -0.513335519569417 -0.845316318615858;
-0.835505052297023 -0.480844385974051 0.0932542737401012 0.581529047501688 0.490268395326362 -0.147666233769839 -0.710649091957449 -0.931003922056647 -0.958925701760676 -0.858819150287956 -0.481661707140576 0.156414285678760 0.447396829361101 0.0981478637058515 -0.451070848447417 -0.819988260893128;
-0.816709047200526 -0.433453278154959 0.159628107720050 0.600427988605026 0.393506424003885 -0.305228888131506 -0.812664548017182 -0.961805915097693 -0.955933291371934 -0.830741003768876 -0.427323324248664 0.207806409950974 0.484525610303648 0.125624661303432 -0.439046713069912 -0.817599757863837;
-0.822861040622790 -0.442896947865577 0.151089884829340 0.580290095271988 0.343247812248396 -0.352033523371175 -0.817086278222730 -0.929514991801673 -0.886978336437167 -0.697090196268156 -0.238002013643830 0.337749616705052 0.498598720531941 0.0760487724596743 -0.486273157847677 -0.843333806408360;
-0.860942986404146 -0.522121527859953 0.0524279712409279 0.526657089668381 0.383930777112517 -0.232692750332808 -0.677325839124602 -0.772547350530417 -0.670540742503032 -0.390616130395944 0.0830334395740294 0.494976504151193 0.448121005982265 -0.0618879799887424 -0.591113408315788 -0.889026249744857;
-0.916802932105578 -0.663201279033345 -0.154044737627659 0.396672170332706 0.491936532473056 0.0940099132498991 -0.286162020065991 -0.364947362766772 -0.214565043562043 0.0881855809237838 0.438209126217266 0.564527435118194 0.265415378457742 -0.296980702008261 -0.738884988307245 -0.940580842425516;
-0.965192802918202 -0.825342497652071 -0.459998558858351 0.0925629855894946 0.480340463722478 0.472442983282266 0.301808949751531 0.258284784197090 0.368708366612420 0.536322394254287 0.603328999537670 0.390529462534692 -0.0949770405459452 -0.592935413235929 -0.880097007927833 -0.978525978461504;
-0.990533430129514 -0.941044035608930 -0.766160549093185 -0.386292155084679 0.0998467598735729 0.450801075602910 0.598055011203918 0.652900359808940 0.665648369691278 0.589058411553531 0.343047097581377 -0.0848816613298983 -0.542900961584228 -0.843998282669678 -0.965353869828169 -0.995255387851856;
-0.998338293978920 -0.988320559344962 -0.941659007321037 -0.799092345375712 -0.517863806528273 -0.155993769402310 0.146441743508075 0.290141712085667 0.245284536321136 0.0243849663389314 -0.309758196452051 -0.642089382998477 -0.866919699790166 -0.966673003354202 -0.994567443789691 -0.999428993951244]
B = [3, 10; 3 5;6 8; 10 10;12 15];

Answers (1)

Image Analyst
Image Analyst on 8 May 2022
Did you try sum()?
A = [-0.999317947480371 -0.993536503585372 -0.969862437773368 -0.906832750196398 -0.759705716591495 -0.481332916950500 -0.153677095108110 0.0141875446062520 -0.0685948868128668 -0.328929988989340 -0.629150891625282 -0.852076097628516 -0.959837865788046 -0.992595983390425 -0.999059474048030 -0.999933713928324;
-0.998496616532260 -0.985598854655850 -0.929901994546247 -0.781710068115769 -0.482597092641038 -0.0362238815467362 0.371217526815227 0.543090608028069 0.478132838616072 0.234246392744947 -0.153302453089086 -0.573278284411362 -0.851351393695826 -0.963688227796016 -0.993762279329382 -0.999337043346916;
-0.995434490728516 -0.967620311535804 -0.863806160702648 -0.612236686974160 -0.186999353246921 0.280766626822551 0.552541331194880 0.586098280009986 0.531486858619764 0.454018934575730 0.230044409279903 -0.204581543975927 -0.639180539482132 -0.885420014372121 -0.974348251331413 -0.996236888546869;
-0.987160499189578 -0.931427299443134 -0.759716812156651 -0.394482792309088 0.108182160202820 0.471188949630731 0.503480340709777 0.335586083931315 0.232335876665255 0.304579043289162 0.358074579227322 0.115659673513168 -0.355231483588663 -0.740679097390359 -0.926078406847594 -0.985677343789221;
-0.971586928949938 -0.872458213033895 -0.610309028929792 -0.132902238738726 0.366390418664472 0.540622560526311 0.325005512271312 -0.0119505594362531 -0.179342191067915 -0.0420368559588870 0.248681795512451 0.297403754620354 -0.0632610056260224 -0.539113882327251 -0.839789031978881 -0.961837090618874;
-0.948091962614030 -0.788313118639847 -0.421881681579890 0.133190346858710 0.538287507930558 0.476064318743290 0.0544066504400661 -0.363909862720283 -0.549869745479283 -0.409558846145971 0.00674225711486494 0.329568725914733 0.165586215561823 -0.323035217203989 -0.726225360511520 -0.925757449921316;
-0.914187288441434 -0.682056788004283 -0.218598709988698 0.352872368633207 0.602817009933680 0.302561572259300 -0.249242626273231 -0.652008419402765 -0.798362057647752 -0.678856137839372 -0.248005715458174 0.262006508209699 0.308918778809463 -0.136236575399137 -0.610968817896169 -0.884121384015371;
-0.872245286353229 -0.571318177056808 -0.0376154150892203 0.502736963155606 0.574275936387609 0.0714784092791476 -0.519492132855641 -0.836145331889220 -0.916248993374835 -0.815080748895835 -0.419315533022597 0.185009018329336 0.393747530022390 0.00623507503954278 -0.513335519569417 -0.845316318615858;
-0.835505052297023 -0.480844385974051 0.0932542737401012 0.581529047501688 0.490268395326362 -0.147666233769839 -0.710649091957449 -0.931003922056647 -0.958925701760676 -0.858819150287956 -0.481661707140576 0.156414285678760 0.447396829361101 0.0981478637058515 -0.451070848447417 -0.819988260893128;
-0.816709047200526 -0.433453278154959 0.159628107720050 0.600427988605026 0.393506424003885 -0.305228888131506 -0.812664548017182 -0.961805915097693 -0.955933291371934 -0.830741003768876 -0.427323324248664 0.207806409950974 0.484525610303648 0.125624661303432 -0.439046713069912 -0.817599757863837;
-0.822861040622790 -0.442896947865577 0.151089884829340 0.580290095271988 0.343247812248396 -0.352033523371175 -0.817086278222730 -0.929514991801673 -0.886978336437167 -0.697090196268156 -0.238002013643830 0.337749616705052 0.498598720531941 0.0760487724596743 -0.486273157847677 -0.843333806408360;
-0.860942986404146 -0.522121527859953 0.0524279712409279 0.526657089668381 0.383930777112517 -0.232692750332808 -0.677325839124602 -0.772547350530417 -0.670540742503032 -0.390616130395944 0.0830334395740294 0.494976504151193 0.448121005982265 -0.0618879799887424 -0.591113408315788 -0.889026249744857;
-0.916802932105578 -0.663201279033345 -0.154044737627659 0.396672170332706 0.491936532473056 0.0940099132498991 -0.286162020065991 -0.364947362766772 -0.214565043562043 0.0881855809237838 0.438209126217266 0.564527435118194 0.265415378457742 -0.296980702008261 -0.738884988307245 -0.940580842425516;
-0.965192802918202 -0.825342497652071 -0.459998558858351 0.0925629855894946 0.480340463722478 0.472442983282266 0.301808949751531 0.258284784197090 0.368708366612420 0.536322394254287 0.603328999537670 0.390529462534692 -0.0949770405459452 -0.592935413235929 -0.880097007927833 -0.978525978461504;
-0.990533430129514 -0.941044035608930 -0.766160549093185 -0.386292155084679 0.0998467598735729 0.450801075602910 0.598055011203918 0.652900359808940 0.665648369691278 0.589058411553531 0.343047097581377 -0.0848816613298983 -0.542900961584228 -0.843998282669678 -0.965353869828169 -0.995255387851856;
-0.998338293978920 -0.988320559344962 -0.941659007321037 -0.799092345375712 -0.517863806528273 -0.155993769402310 0.146441743508075 0.290141712085667 0.245284536321136 0.0243849663389314 -0.309758196452051 -0.642089382998477 -0.866919699790166 -0.966673003354202 -0.994567443789691 -0.999428993951244]
A = 16×16
-0.9993 -0.9935 -0.9699 -0.9068 -0.7597 -0.4813 -0.1537 0.0142 -0.0686 -0.3289 -0.6292 -0.8521 -0.9598 -0.9926 -0.9991 -0.9999 -0.9985 -0.9856 -0.9299 -0.7817 -0.4826 -0.0362 0.3712 0.5431 0.4781 0.2342 -0.1533 -0.5733 -0.8514 -0.9637 -0.9938 -0.9993 -0.9954 -0.9676 -0.8638 -0.6122 -0.1870 0.2808 0.5525 0.5861 0.5315 0.4540 0.2300 -0.2046 -0.6392 -0.8854 -0.9743 -0.9962 -0.9872 -0.9314 -0.7597 -0.3945 0.1082 0.4712 0.5035 0.3356 0.2323 0.3046 0.3581 0.1157 -0.3552 -0.7407 -0.9261 -0.9857 -0.9716 -0.8725 -0.6103 -0.1329 0.3664 0.5406 0.3250 -0.0120 -0.1793 -0.0420 0.2487 0.2974 -0.0633 -0.5391 -0.8398 -0.9618 -0.9481 -0.7883 -0.4219 0.1332 0.5383 0.4761 0.0544 -0.3639 -0.5499 -0.4096 0.0067 0.3296 0.1656 -0.3230 -0.7262 -0.9258 -0.9142 -0.6821 -0.2186 0.3529 0.6028 0.3026 -0.2492 -0.6520 -0.7984 -0.6789 -0.2480 0.2620 0.3089 -0.1362 -0.6110 -0.8841 -0.8722 -0.5713 -0.0376 0.5027 0.5743 0.0715 -0.5195 -0.8361 -0.9162 -0.8151 -0.4193 0.1850 0.3937 0.0062 -0.5133 -0.8453 -0.8355 -0.4808 0.0933 0.5815 0.4903 -0.1477 -0.7106 -0.9310 -0.9589 -0.8588 -0.4817 0.1564 0.4474 0.0981 -0.4511 -0.8200 -0.8167 -0.4335 0.1596 0.6004 0.3935 -0.3052 -0.8127 -0.9618 -0.9559 -0.8307 -0.4273 0.2078 0.4845 0.1256 -0.4390 -0.8176
imshow(A, []);
B = [1 3 5 19 20 30 80 90];
redPixelValues = A(B)
redPixelValues = 1×8
-0.9993 -0.9954 -0.9716 -0.9676 -0.9314 -0.8253 -0.5179 -0.3052
theSum = sum(redPixelValues)
theSum = -6.5138
  2 Comments
z cy
z cy on 8 May 2022
I want to draw the figure as i shownd. The problem is that i dont kown how to draw red point according to index B.
Image Analyst
Image Analyst on 8 May 2022
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
markerSize = 30;
fprintf('Beginning to run %s.m ...\n', mfilename);
hFig = figure;
A = [-0.999317947480371 -0.993536503585372 -0.969862437773368 -0.906832750196398 -0.759705716591495 -0.481332916950500 -0.153677095108110 0.0141875446062520 -0.0685948868128668 -0.328929988989340 -0.629150891625282 -0.852076097628516 -0.959837865788046 -0.992595983390425 -0.999059474048030 -0.999933713928324;
-0.998496616532260 -0.985598854655850 -0.929901994546247 -0.781710068115769 -0.482597092641038 -0.0362238815467362 0.371217526815227 0.543090608028069 0.478132838616072 0.234246392744947 -0.153302453089086 -0.573278284411362 -0.851351393695826 -0.963688227796016 -0.993762279329382 -0.999337043346916;
-0.995434490728516 -0.967620311535804 -0.863806160702648 -0.612236686974160 -0.186999353246921 0.280766626822551 0.552541331194880 0.586098280009986 0.531486858619764 0.454018934575730 0.230044409279903 -0.204581543975927 -0.639180539482132 -0.885420014372121 -0.974348251331413 -0.996236888546869;
-0.987160499189578 -0.931427299443134 -0.759716812156651 -0.394482792309088 0.108182160202820 0.471188949630731 0.503480340709777 0.335586083931315 0.232335876665255 0.304579043289162 0.358074579227322 0.115659673513168 -0.355231483588663 -0.740679097390359 -0.926078406847594 -0.985677343789221;
-0.971586928949938 -0.872458213033895 -0.610309028929792 -0.132902238738726 0.366390418664472 0.540622560526311 0.325005512271312 -0.0119505594362531 -0.179342191067915 -0.0420368559588870 0.248681795512451 0.297403754620354 -0.0632610056260224 -0.539113882327251 -0.839789031978881 -0.961837090618874;
-0.948091962614030 -0.788313118639847 -0.421881681579890 0.133190346858710 0.538287507930558 0.476064318743290 0.0544066504400661 -0.363909862720283 -0.549869745479283 -0.409558846145971 0.00674225711486494 0.329568725914733 0.165586215561823 -0.323035217203989 -0.726225360511520 -0.925757449921316;
-0.914187288441434 -0.682056788004283 -0.218598709988698 0.352872368633207 0.602817009933680 0.302561572259300 -0.249242626273231 -0.652008419402765 -0.798362057647752 -0.678856137839372 -0.248005715458174 0.262006508209699 0.308918778809463 -0.136236575399137 -0.610968817896169 -0.884121384015371;
-0.872245286353229 -0.571318177056808 -0.0376154150892203 0.502736963155606 0.574275936387609 0.0714784092791476 -0.519492132855641 -0.836145331889220 -0.916248993374835 -0.815080748895835 -0.419315533022597 0.185009018329336 0.393747530022390 0.00623507503954278 -0.513335519569417 -0.845316318615858;
-0.835505052297023 -0.480844385974051 0.0932542737401012 0.581529047501688 0.490268395326362 -0.147666233769839 -0.710649091957449 -0.931003922056647 -0.958925701760676 -0.858819150287956 -0.481661707140576 0.156414285678760 0.447396829361101 0.0981478637058515 -0.451070848447417 -0.819988260893128;
-0.816709047200526 -0.433453278154959 0.159628107720050 0.600427988605026 0.393506424003885 -0.305228888131506 -0.812664548017182 -0.961805915097693 -0.955933291371934 -0.830741003768876 -0.427323324248664 0.207806409950974 0.484525610303648 0.125624661303432 -0.439046713069912 -0.817599757863837;
-0.822861040622790 -0.442896947865577 0.151089884829340 0.580290095271988 0.343247812248396 -0.352033523371175 -0.817086278222730 -0.929514991801673 -0.886978336437167 -0.697090196268156 -0.238002013643830 0.337749616705052 0.498598720531941 0.0760487724596743 -0.486273157847677 -0.843333806408360;
-0.860942986404146 -0.522121527859953 0.0524279712409279 0.526657089668381 0.383930777112517 -0.232692750332808 -0.677325839124602 -0.772547350530417 -0.670540742503032 -0.390616130395944 0.0830334395740294 0.494976504151193 0.448121005982265 -0.0618879799887424 -0.591113408315788 -0.889026249744857;
-0.916802932105578 -0.663201279033345 -0.154044737627659 0.396672170332706 0.491936532473056 0.0940099132498991 -0.286162020065991 -0.364947362766772 -0.214565043562043 0.0881855809237838 0.438209126217266 0.564527435118194 0.265415378457742 -0.296980702008261 -0.738884988307245 -0.940580842425516;
-0.965192802918202 -0.825342497652071 -0.459998558858351 0.0925629855894946 0.480340463722478 0.472442983282266 0.301808949751531 0.258284784197090 0.368708366612420 0.536322394254287 0.603328999537670 0.390529462534692 -0.0949770405459452 -0.592935413235929 -0.880097007927833 -0.978525978461504;
-0.990533430129514 -0.941044035608930 -0.766160549093185 -0.386292155084679 0.0998467598735729 0.450801075602910 0.598055011203918 0.652900359808940 0.665648369691278 0.589058411553531 0.343047097581377 -0.0848816613298983 -0.542900961584228 -0.843998282669678 -0.965353869828169 -0.995255387851856;
-0.998338293978920 -0.988320559344962 -0.941659007321037 -0.799092345375712 -0.517863806528273 -0.155993769402310 0.146441743508075 0.290141712085667 0.245284536321136 0.0243849663389314 -0.309758196452051 -0.642089382998477 -0.866919699790166 -0.966673003354202 -0.994567443789691 -0.999428993951244]
imshow(A, [], 'InitialMagnification', 1000);
axis('on', 'image');
hFig.WindowState = 'maximized';
B = [1 3 5 19 20 30 80 90];
% Plot red squares
hold on;
[y, x] = ind2sub(size(A), B)
scatter(x, y, 400, 'r', 'filled', 'Marker', 's')
redPixelValues = A(B)
theSum = sum(redPixelValues)

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!