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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:22:35+00:00 2026-06-10T22:22:35+00:00

I have a matrix say Z = [1 2 3; 4 5 6; 7

  • 0

I have a matrix say

Z = [1 2 3;
     4 5 6;
     7 8 9]

I have to change its values, say at positions (2,2) and (3,1), to some specified value. I have two matrices rowNos and colNos which contain these positions:

rowNos = [2, 3]
colNos = [2, 1]

Let’s say I want to change the value of elements at these positions to 0.

How can I do it without using for loop?

  • 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-10T22:22:36+00:00Added an answer on June 10, 2026 at 10:22 pm

    Use sub2ind, it’ll convert your sub-indices to linear indices, which is a number pointing at one exact spot in the matrix (more info).

    Z = [ 1 2 3 ; 4 5 6 ; 7 8 9];
    rowNos = [2, 3];
    colNos = [2, 1];
    
    lin_idcs = sub2ind(size(Z), rowNos, colNos)
    

    If you want to operate on all elements on a specific row and column (elements in higher dimensions that is), you can also address them using linear indexing. It only becomes a bit trickier of calculating them:

    Z = reshape(1:4*4*3,[4 4 3]);
    rowNos = [2, 3];
    colNos = [2, 1];
    
    siz = size(Z);
    lin_idcs = sub2ind(siz, rowNos, colNos,ones(size(rowNos))); % just the first element of the remaining dimensions
    lin_idcs_all = bsxfun(@plus,lin_idcs',prod(siz(1:2))*(0:prod(siz(3:end))-1)); % all of them
    lin_idcs_all = lin_idcs_all(:);
    
    Z(lin_idcs_all) = 0;
    

    experiment a bit with sub2ind, and go through my code step-by-step to understand it.

    It would’ve been easier if it was the first dimension you wanted to take all elements off, then you could have used the colon operator :

    Z = reshape(1:3*4*4,[3 4 4]);
    rowNos = [2, 3];
    colNos = [2, 1];
    
    siz = size(Z);
    lin_idcs = sub2ind(siz(2:end),rowNos,colNos);
    Z(:,lin_idcs) = 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a matrix ( MxN ) which has its rows and columns
Let's say I have a matrix x which contains 10 rows and 2 columns.
Say I have some matrix, for example: m = matrix(rep(c(0, 0, 1), 4), nrow
Let's say I have a matrix of values that I want to populate, that
Say I have a matrix of values set.seed(1) A <- matrix(runif(25),ncol=5) I'd like to
Let's say I have a matrix with all 0's with the exception of the
Say in Matlab I have a matrix like this: 1 2 3 4 5
So let's say I have a matrix, mdat and I only know the index
Say I have the following matrix: A = randi(10, [6 3]) 7 10 3
I have a 2D matrix, say M=zeros(10,10); I have another column matrix, V=[1;2;3;4;5;6;5;4;3;2]; I

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.