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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:53:59+00:00 2026-05-25T16:53:59+00:00

I have a question about modifying a matrix. I have managed to change values

  • 0

I have a question about modifying a matrix. I have managed to change values in a matrix depending on the value
of another matrix when they are of the same dimension. But now I need to apply this procedure to matrixes with
different dimensions. In other words, I want to apply some changes to a “region” of the bigger matrix depending
on the values of the smaller one, bearing in mind that I know the positions of the smaller matrix associated
to the bigger one.

Suppose this are matrix A

1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 

and B

 0 0 0 0 0 0 
 0 1 1 1 1 0 
 0 0 1 1 1 0 
 0 0 1 1 0 0 
 0 0 0 0 0 0 

I know that B[1,1] is the value I have to check to modify A[2,1], B[2,1] for A[3,1] and so on…

The final result I am looking for is

1 2 3 4 5 6 7 8 
1 2 0 0 0 0 7 8 
1 2 3 0 0 0 7 8 
1 2 3 0 0 6 7 8 
1 2 3 4 5 6 7 8 

For the A values replacement I use a for loop in my original script

for (i in 1:10) A[B == i] = 0

that works when A and B have same dimension.

How should I make the replacement in matrix A? apply? a for loop?

Any help would be appreciated and of course you can point me to some basic reading I haven’t still read.

  • 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-25T16:53:59+00:00Added an answer on May 25, 2026 at 4:53 pm

    Just select the submatrix and do what you want, i.e.:

    A[1:5,2:8][B == 1] = 0
    

    Or, in general, if [row, col] is the starting position of matrix B in matrix A:

    A[row:nrow(A),col:ncol(A)][B == 1] = 0
    

    Here is the test code so that you can check it works:

    A = matrix(rep(1:8, each = 5), nrow = 5)
    B = matrix(c(rep(0, 7), 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, rep(0, 8)), nrow = 5, byrow = TRUE)
    A[1:5,2:8][B == 1] = 0
    

    Thanks to Andrie ho for hinting me the ncol function instead of ugly dim(A)[2]…

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

Sidebar

Related Questions

I have a question about this question . I posted a reply there but
I have a question about this script I found and used. It works but
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
I have question about normalization. Suppose I have an applications dealing with songs. First
I have a question about using streams in .NET to load files from disk.
I have a question about best practices regarding how one should approach storing complex
I have a question about locking. This doesn't have to be only about record
I have a question about how to deploy WPF application into a PC without
I have a question about using os.execvp in Python. I have the following bit
I have a question about using new[] . Imagine this: Object.SomeProperty = new[] {string1,

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.