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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:19:11+00:00 2026-06-01T16:19:11+00:00

I want to create different matrices in a loop and then combine (either cbind

  • 0

I want to create different matrices in a loop and then combine (either cbind or rbind) them. But the following codes doesn’t work. Why not? And how to fix it?

dependent = matrix(c(30,184,6,106), 2, 2, byrow=T)
independent = c(160,166)
expected = numeric()
{for(i in 1:length(independent))
a = dependent*independent[i]/sum(independent)
expected = cbind(expected,a)}

This gives:

expected
        [,1]     [,2]
[1,] 15.276074 93.69325
[2,]  3.055215 53.97546

This is the result of only using the final iteration of the for loop. So the result is like only 166 is used, but 160 isn’t.

  • 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-01T16:19:13+00:00Added an answer on June 1, 2026 at 4:19 pm

    A few comments:

    1. Your for loop brackets are in the wrong place. You have:

      R> {for(i in 1:3) 
      +   cat(i, "\n")
      +   cat(i, "\n")
      + }
      1 
      2 
      3 
      3 
      

      instead you should have:

      R> for(i in 1:3) {
      +   cat(i, "\n")
      +   cat(i, "\n")
      + }
      1 
      1 
      2 
      2 
      3 
      3 
      

      When you construct a for loop and ommit the brackets, only the first line after the for statement is used.

    2. You can make your for loop more efficient by saving the result of sum(independent) since that doesn’t change with each iteration, i.e.

        for(i in 1:length(independent)){
            a = dependent*independent[i]
            expected = cbind(expected,a)
         }
         expected  = expected//sum(independent)
      
    3. In fact you can vectorise the whole calculation

      y = sapply(independent, '*', dependent)
      matrix(y, ncol=4,nrow=2)/sum(independent)
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

for a localized website I want to create different language files. But my main
I want to create an overlay on top of Google Maps that displays different
I want to create a method sum that I can call on different types,
I want to create a website with various users. The users can have different
I want to create a FlipPanel, which provides two different views of the same
I want to store the distance between different locations in a table. CREATE TABLE
I want to create different inputmasks like Date Input mask in Javascript. What will
Say I have three different 2x2 submatrices, and I want to create a big
For example, I have 6 similar Matrices (Matrix A1 to A6) but with different
I want to create different kinds of processes in which agents interact with each

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.