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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:22:29+00:00 2026-05-28T08:22:29+00:00

I have a vectorization Q in R using matrices. I have 2 Cols that

  • 0

I have a vectorization Q in R using matrices. I have 2 Cols that need to be regressed against each using certain indices. Data is

matrix_senttoR = [ ...
                  0.11 0.95
                  0.23 0.34
                  0.67 0.54
                  0.65 0.95
                  0.12 0.54
                  0.45 0.43 ] ;
indices_forR = [ ...
            1
            1
            1
            2
            2
            2 ] ;

Col1 in matrix is data for say MSFT and GOOG (3 rows each) and Col2 is the return from benchmark StkIndex, on corresponding dates. The data is in matrix format as it is sent from Matlab.

I currently use

slope <- by(    data.frame(matrix_senttoR),   indices_forR,   FUN=function(x)  
                         {zyp.sen (X1~X2,data=x) $coeff[2] }      ) 
betasFac <- sapply(slope , function(x) x+0)

I’m using data.frame above as I could not use cbind(). If I use cbind() then Matlab gives an error as it doesn’t understand that format of data. I’m running these commands from inside Matlab (http://www.mathworks.com/matlabcentral/fileexchange/5051). You can replace zyp (zyp.sen) with lm.

BY is slow here (may be because of dataframes?). Is there a better way to do it? It takes 14secs+ for 150k rows of data. Can I instead use matrix-vectorization in R? Thanks.

  • 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-28T08:22:30+00:00Added an answer on May 28, 2026 at 8:22 am

    I still think that you are overcomplicating things by moving from MATLAB to R and back. And passing 150k rows of data must be slowing things down considerably.

    zyp.sen is actually pretty trivial to port to MATLAB. Here you go:

    function [intercept, slope, intercepts, slopes, rank, residuals] = ZypSen(x, y)
    % Computes a Thiel-Sen estimate of slope for a vector of data.
    
    n = length(x);
    
    slopes = arrayfun(@(i) ZypSlopediff(i, x, y, n), 1:(n - 1), ...
        'UniformOutput', false);
    slopes =  [slopes{:}];
    sni = isfinite(slopes);
    slope = median(slopes(sni));
    
    intercepts = y - slope * x;
    intercept = median(intercepts);
    
    rank = 2;
    residuals = x - slope * y + intercept;
    
    end
    
    
    function z = ZypSlopediff(i, x, y, n)
    
    z = (y(1:(n - i)) - y((i + 1):n)) ./ ...
        (x(1:(n - i)) - x((i + 1):n));
    
    end
    

    I checked this using the R’s example(zyp.sen), and it gives the same answer.

    x = [0 1 2 4 5]
    y = [6 4 1 8 7]
    [int, sl, ints, sls, ra, res] = ZypSen(x, y)
    

    You should really do some further checking though, just to be sure.

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

Sidebar

Related Questions

Have A really simple form that isnt sending any data back to my controller.
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have approximately 5,000 matrices with the same number of rows and varying numbers
I have an image processing algorithm which makes of matrices, I have my own
Have some code: using (var ctx = new testDataContext()) { var options = new
Have a look at one of my websites: moskah.com The problem is that it
I realize that this is a question that will probably not have a single
I have a matrix A with size (nr,nc), a vector of column indices B
Have some dates in my local Oracle 11g database that are in this format:

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.