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

  • SEARCH
  • Home
  • 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 4583472
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:13:10+00:00 2026-05-21T21:13:10+00:00

This is very closely related to this other question , but that question wanted

  • 0

This is very closely related to this other question, but that question wanted to avoid sub2ind because of performance concerns. I am more concerned about the “unelegance” of using sub2ind.

Let’s suppose I want to create another MxN matrix which is all zeros except for one entry in each column that I want to assign from the corresponding entry in a vector, and choice of row in each column is based on another vector. For example:

z = zeros(10,4);
rchoice = [3 1 8 7];
newvals = [123 456 789 10];
% ??? I would like to set z(3,1)=123, z(1,2)=456, z(8,3)=789, z(7,4)=10

I can use sub2ind to accomplish this (which I used in an answer to a closely related question):

z(sub2ind(size(z),rchoice,1:4)) = newvals

but is there another alternative? Seems like logical addressing could be used in some way but I’m stumped, because in order to set the elements of a logical matrix to 1, you’re dealing with the same element positions as in the matrix you actually want to address.

  • 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-21T21:13:12+00:00Added an answer on May 21, 2026 at 9:13 pm

    There’s a much simpler way of doing it.

    nCols=size(z,2);
    z(rchoice,1:nCols)=diag(newvals);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very closely related to SQL - Select most 'active' timespan fromdb but different question.
This is closely related to anothewr question I have asked but since this is
So I have a question very closely related to another question I've seen on
This question relates very closely to Can a nested C++ class inherit its enclosing
This question comes very closely to what I'm after: Replace an Attribute in the
I suppose this is two very closely related questions: Everyone says to start with
I have a requirement that very closely matches this Jquery demo , which is
I'm using this very handy plugin to sort elements. But comparing elements on a
I have this very simple br2nl function that I use to take a string
I have a question which closely relates to this question . In my schema

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.