How to draw this figure?
    3 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
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];
0 commentaires
Réponses (1)
  Image Analyst
      
      
 le 8 Mai 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]
imshow(A, []);
B = [1 3 5 19 20 30 80 90];
redPixelValues = A(B)
theSum = sum(redPixelValues)
2 commentaires
  Image Analyst
      
      
 le 8 Mai 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)

Voir également
Catégories
				En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


