Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6643391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:02:43+00:00 2026-05-26T00:02:43+00:00

I have implemented a function to do this already, but I don’t like the

  • 0

I have implemented a function to do this already, but I don’t like the necessity for a loop. I don’t see any way to avoid the loop however. Curious if there is a better way to do this.

function [ colored_img ] = colorImg ( img, ix, c )
% function [ colored_img ] = colorImg ( img, ix, c )
% 
% INPUTS
%   img - a 3D array representing the image to color.  
%   ix  - the indexes of the pixels to set to a new color.
%   c   - a vector representing the color to paint the pixels ix.
%
% OUTPUTS
%   colored_img - the colored image.


colored_img = img;
for jx = 1 : numel(c);
    a = colored_img(:,:,jx);
    a(ix) = c(jx);
    colored_img(:,:,jx) = a;
end




end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:02:43+00:00Added an answer on May 26, 2026 at 12:02 am

    You can just replicate the indices for the colors.

    imageSize = numel(img(:,:,1));
    rgbIdx = bsxfun(@plus,ix(:),(0:2)*imageSize);
    
    %# replicate the color so that there are as many 
    %# entries as pixels to recolor
    %# (skip this if you use a colormap
    %#  of length nPixelsToRecolor)
    nPixelsToRecolor = length(ix); %# assign this so that comment makes sense
    repColor = repmat(c,nPixelsToRecolor,1); %# assuming color is 1-by-3
    
    colored_img = img;
    colored_img(rgbIdx(:)) = repColor(:);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I know this seems like a duplicate question, but don't think it is.
I have implemented this function: static <X,Y> Y castOrNull(X obj) { try { return
I have implemented authentication systems for webapps several times over the years, but before
I have already implemented some AJAX pagination in my Rails app by using the
What is the most elegant way to implement this function: ArrayList generatePrimes(int n) This
I have come across this great function/command . Colour to RGB, you can do
I have a base class like this class Base { public var space:Number; }
If I have a subclass that has yet to implement a function provided by
I have implemented what I thought was a pretty decent representation of MVC in
I have implemented a simple file upload-download mechanism. When a user clicks a file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.