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 8723427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:37:25+00:00 2026-06-13T07:37:25+00:00

I have a Matlab Code Snippet and I try to write in C++, but

  • 0

I have a Matlab Code Snippet and I try to write in C++, but I really don’t understand
what is happening there:

for c = 1:3
    Id = double(I(:,:,c))/255;
    Wc(:,:,c) = sum(Id(pixels).*weights, 3);
end

There is an image I of size 480x640x3. In the first Iteration the first channel of the image is
saved in Id, which then has a size of 480×640. But what is happening in the next line? I just don’t
understand that syntax.

pixels and weights are of size 300x383x4x1.

So what does this line exactly do?:

sum(Id(pixels).*weights, 3);

Thank you.

  • 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-06-13T07:37:26+00:00Added an answer on June 13, 2026 at 7:37 am

    sum(X, n) summarizes all elements of a matrix X across the n-th dimension, so this:

    sum(Id(pixels) .* weights, 3);
    

    really does the following:

    • Extracts a sub-matrix from matrix Id, pixels being the indices of the extracted values:
      matrix Id is imlicitly converted to a column vector, and the resulting sub-matrix is of the same size as pixels (each value being equal to Id(p), where p is the corresponding element in pixels).
    • Multiplies the extracted matrix by another matrix weights element-by-element (note that the multiplication operator is .*)
    • Summarizes all elements of the resulting 3-D matrix along the third dimension.

    You can look into the official documentation for more information.

    EDIT: A Simple Example

    Let’s assume that:

    Id = [10 20; 30 40; 50 60; 70 80];
    pixels(:, :, 1) = [4 4; 4 4];
    pixels(:, :, 2) = [6 6; 6 6];
    pixels(:, :, 3) = [8 8; 8 8];
    

    that is, Id is a 2-D matrix, and pixels is 3-D. Now, Id(pixels) would yield:

    ans(:, :, 1) =
    
      70  70
      70  70
    
    ans(:, :, 2) =
    
      40  40
      40  40
    
    ans(:, :, 3) =
    
      80  80
      80  80
    

    because if you convert Id to a column vector (try Id(:)), 70 is the 4th element, 40 is the 6th, and 80 is the 8th. Note that the result has the same dimensions as pixels (not Id!).

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code that works in Matlab version R2010a but shows errors in
I have the following code snippet in Matlab and want to port it to
I have some MATLAB code I've been working with. There are three sections, say
I'm looking at some Matlab code (don't have the software). I'm wondering what the
I have a working MATLAB code, which works fine on another machine, but when
I have tried, just for the fun of it, to write a MatLab-code for
I have a piece of matlab code below which reads data from a table.
I have been given some matlab code compiled using the .net compiler. I can
I have found this line of MatLab code on the internet that displays a
I have developed code on a m-file script in Matlab that communicates with a

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.