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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:01:03+00:00 2026-05-23T17:01:03+00:00

I have two matrices I want to perform a loop on. My problem is

  • 0

I have two matrices I want to perform a loop on. My problem is I am looping for one colomn and do not know how to include the other column, hence my incorrect results. My codes are as below:

t=as.matrix(b)
y=as.matrix(a)
t
            a     b 
      [1,]  1     10
      [2,] NA      9
      [3,]  3     NA
      [4,]  4      7
      [5,]  5      6
      [6,]  3      4

y
              c      d
      [1,]    3     12
      [2,]    NA     11
      [3,]    5     NA
      [4,]    6      9
      [5,]    7      8
      [6,]    3     12

turn
     [,1]    [,2]
[1,]    0      0
[2,]    0      0

Code:

n=3  #number to consider at a time
runs=2 #total data points divided by 60 to the nearest whole number
turn=matrix(0, nrow=runs,2)
TR = y/t

for (i in 1: runs){
  index_start=3*(i-1)+1
  index_end= 3*i
  turn[i]=mean( TR[index_start:index_end],na.rm=TRUE)
}



turn
     [,1]      [,2]
[1,] 2.333333    0
[2,] 1.300000    0

The turn output has given correct results for the first column but, as expected, incorrect results for the second column. How do I adjust my loop function? Thank you in advance.

  • 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-23T17:01:03+00:00Added an answer on May 23, 2026 at 5:01 pm

    Look at the line

    turn[i]=mean( TR[index_start:index_end],na.rm=TRUE)
    

    If you only give a single number in the index of a matrix, it will work out the position by counting down the first column, then down the second, and so on.

    So turn[1] refers to the element in the top left corner of the matrix turn, and turn[2] refers to the bottom left corner.

    In your loop, i takes the values 1:runs, and runs is 2, so you only assign things to the first two elements of turn.

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

Sidebar

Related Questions

I have two 3x3 matrices. One representing the actual state and the other in
I have two classes, and want to include a static instance of one class
Here's an interesting question :) I have two vectors of matrices which I want
say I want to multiply two matrices together, 50 by 50. I have 2
Here is my code...I have two dimensional matrices A,B. I want to develop the
I have two n-by-m matrices, A and B . I want to create a
I have two 50 x 6 matrices, say A and B . I want
I have two matrices that I want to apply a function to, by rows:
I want to multiply two matrices but the triple loop has O(n 3 )
I have two .csv files containing correlation matrices exported from R. One file contains

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.