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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:47:49+00:00 2026-06-12T10:47:49+00:00

How to do X * diag(Y) in Scala Breeze? X could be for example

  • 0

How to do X * diag(Y) in Scala Breeze? X could be for example a CSCMatrix and Y could be a DenseVector?

In MATLAB syntax, this would be:

X * spdiags(0, Y, N, N )

Or:

X .* repmat( Y', K, 0 )

In SciPy syntax, this would be a ‘broadcast multiply’:

Y * X

How to do X * diag(Y) in Scala Breeze?

  • 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-12T10:47:50+00:00Added an answer on June 12, 2026 at 10:47 am

    I wrote my own sparse diagonal method, and dense / sparse multiplication method in the end.

    Use like this:

    val N = 100000
    val K = 100
    val A = DenseMatrix.rand(N,K)
    val b = DenseVector.rand(N)
    val c = MatrixHelper.spdiag(b)
    val d = MatrixHelper.mul( A.t, c )
    

    Here are the implementations of spdiag and mul:

    // Copyright Hugh Perkins 2012
    // You can use this under the terms of the Apache Public License 2.0
    // http://www.apache.org/licenses/LICENSE-2.0
    
    package root
    
    import breeze.linalg._
    
    object MatrixHelper {
       // it's only efficient to put the sparse matrix on the right hand side, since 
       // it is a column-sparse matrix
       def mul( A: DenseMatrix[Double], B: CSCMatrix[Double] ) : DenseMatrix[Double] = {
          val resultRows = A.rows
          val resultCols = B.cols
          var row = 0
          val result = DenseMatrix.zeros[Double](resultRows, resultCols )
          while( row < resultRows ) {
             var col = 0
             while( col < resultCols ) {
                val rightRowStartIndex = B.colPtrs(col)
                val rightRowEndIndex = B.colPtrs(col + 1) - 1
                val numRightRows = rightRowEndIndex - rightRowStartIndex + 1
                var ri = 0
                var sum = 0.
                while( ri < numRightRows ) {
                   val inner = B.rowIndices(rightRowStartIndex + ri)
                   val rightValue = B.data(rightRowStartIndex + ri)
                   sum += A(row,inner) * rightValue
                   ri += 1
                }
                result(row,col) = sum
                col += 1
             }
             row += 1
          }
          result
       }   
    
       def spdiag( a: Tensor[Int,Double] ) : CSCMatrix[Double] = {
          val size = a.size
          val result = CSCMatrix.zeros[Double](size,size)
          result.reserve(a.size)
          var i = 0
          while( i < size ) {
             result.rowIndices(i) = i
             result.colPtrs(i) = i
             result.data(i) = i
             //result(i,i) = a(i)
             i += 1
          }
          //result.activeSize = size
          result.colPtrs(i) = i
          result
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

tesconf is 3x3 matrix; tes_avg = (diag(tesconf)./sum(tesconf,2)); example that the result of tes_avg is
In Matlab, this seems to be more of algo question but can someone explain
I use WPF diagram component from MindFusion (MindFusion.Diagramming.Wpf.Diagram). XAML: ... <Grid> <diag:Diagram x:Name=diagram/> </Grid>
I am trying to access this C++ function from the C# code in my
I want to vectorize the following MATLAB code. I think it must be simple
My app pools keep randomly crashing in IIS 6.0 MS Debug Diag points to
How would I go about creating the matrix [1 2 0 0 0; -1
I am wondering what would be the best way to vectorize the following formula:
I'm trying to implement hough transform for lines in matlab. And I have been
I have a small problem in coding confusion 3x3 matrix by using matlab... I

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.