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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:57:38+00:00 2026-05-23T18:57:38+00:00

May you correct my loop function. I want to calculate correlation of colomns of

  • 0

May you correct my loop function. I want to calculate correlation of colomns of my matrices a and b. The output should be a loop of correlation of the first 3 rows of a and b and then the remaining 3(not a correlation matrix). My code is below.

a=read.table("H:/cor1.txt",header=T)
b=read.table("H:/cor2.txt",header=T)
t=as.matrix(a)
y

          d     e    f
   [1,]   6    -5    7
   [2,]   7    -4    4
   [3,]   8    -3    3
   [4,]   9    -2    3
   [5,]  10    -1    9
   [6,]  11     0    7


t
         a     b    c
  [1,]   1    -1    4
  [2,]   2    -2    6
  [3,]   3    -3    9
  [4,]   4    -4   12
  [5,]   5    -5    6
  [6,]   6    -6    5

y=as.matrix(b)
n=3  #number to consider at a time
runs=2 #runs multiplied by number of shares be looked at
Corrs=matrix(0, nrow=2,3) #100 is number of shares being looked at
for (i in 1:runs){
    index_start = n*(i-1)+1  #replace 100 with days in a quater
    index_end = n*i  #replace 100 with days in a quater
    use_index = index_start:index_end
    Corrs[i] = diag(cor(t[use_index,],y[use_index,]))
}

Warning messages:

1: In Corrs[i] = diag(cor(t[use_index, ], y[use_index, ])) :
  number of items to replace is not a multiple of replacement length
2: In Corrs[i] = diag(cor(t[use_index, ], y[use_index, ])) :
  number of items to replace is not a multiple of replacement length

Corrs

     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    1    0    0

the correct answer should be a matrix with the two rows below, respectively

diag(cor(t[1:3,],y[1:3,]))

[1]  1.0000000 -1.0000000 -0.9226129

diag(cor(t[4:6,],y[4:6,]))

[1]  1.0000000 -1.0000000 -0.8934051
  • 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-23T18:57:38+00:00Added an answer on May 23, 2026 at 6:57 pm

    on corrs[i] , change it to corrs[i,]

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

Sidebar

Related Questions

I may be misunderstanding the correct usage of this function, but I believe appendChild
This may be super easy to solve but I can't find the correct google
Hey all, title may be abit misleading but i didnt know the correct way
May I install a search provider/accelerator remotely in Internet Explorer 8? I dont want
(im using MVC4 VB). I want a FOR .. NEXT loop in my T4
To whom it may respond to, We would like to use SELECT function with
I tried saving the datatable rows to a datarow[] array then deleting the datatable
In each iteration of a loop, I am calculating a MATLAB matrix. These matrices
I want to map pointer to integer for purpose of serialization. The pointers may
May I know how could I pass the value of driver from code behind

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.