Given an ArrayList (rows) of ArrayList (elements in a row), is there a better way to obtain a list of sums like (row[0][0] + row[1][0] + .... row[n][0]) except writing a "for" loop?
Kotlin: Elegant way to sum elements across the rows in matrix?
1.5k Views Asked by Arnie Schwarzvogel At
2
There are 2 best solutions below
Related Questions in MATRIX
- Setting diagonal of a matrix to zero
- CUDA matrix inversion
- Function to create matrix of zeros and ones, with a certain density of ones
- DirectX 9 With No SDK Installed - How To Translate a D3DMATRIX?
- Using the sympy module to compute the matrix multiplication involving symbols
- Rendering a visualisation of matrix using pygame
- I do not receive iOS push notifications from Element Matrix Notify
- Matrix reconstruction by SVD in tensorflow
- Why does the following code detect this matrix as a non-singular matrix?
- Bound for product of matrices
- iterating through raster bands to perform calculation
- How to make a heatmap and the matrix for it?
- MATLAB: Turn every element of complex matrix into another matrix
- Matrix calculated based on the previous value
- Matlab array of structure
Related Questions in KOTLIN
- getting error when trying to launch kotlin jar file that use supabase "java.lang.NoClassDefFoundError"
- ussd reader in Recket Native module
- making android analyze with coverity sast tool
- Jooq - Insert does not update object with generated id
- Doesn't work TrafficStats.clearThreadStatsTag() in Kotlin
- Difference between map and function returning when in Kotlin
- Every Time i run the app it gives me an error related to gradle
- How Do I Create A Path In The pubspec.yaml File?
- Android jetpack compose webview, image selector not works
- Why doesn't CIOEngine have a dispatcher in kotlin script?
- preventing duplicate text
- onCompletion(mp: MediaPlayer?) is unintentionally called after USB storage is connected
- When using a Room database on an Android application, is it possible to pre-populate data
- Jetpack compose - how I can hookup LiveData property from ViewModel with TextField in activity?
- Failed to getEnergyData
Related Questions in FUNCTIONAL-PROGRAMMING
- On Google Sheets (and only built-in functions allowed, no Google Apps Script) Is it possible to simulate pipe function?
- Why does Enum require to implement toEnum and fromEnum, if that's not enough for types larger than Int?
- Is there a functional way to map a list (N elements) to a list of sums of adjacent elements (N - 1 elements) in Kotlin?
- How to count the occurences of every element in a list in Haskell fast?
- Combine lists with absolute index in functional programming
- How to refactor a loop with iterator. (Returning from closure)
- In Haskell, what does `Con Int` mean?
- Setting up different Java class fields value by a single value on some counter value
- Why doesn't map read show (Integer) work to separate each value in a string of Integers?
- Grouping by multiple fields and counting using in Java 8
- Variable capture: How variables behave in function closures
- Composing React Providers with Value props in Typescript
- How can atomicModifyIORef cause leaks? And why does atomicModifyIORef' solve the problem?
- How can I change XMobar's Kbd monitor plugin such that clicking on it loops throught the layouts?
- How to get success or error data without folding the response while using fpdart in flutter?
Related Questions in MAPPING
- Hospital route finding ai project
- ImportError: cannot import name 'Mapping' from 'collections' (E:\Anaconda\envs\nlp\Lib\collections\__init__.py)
- How to exclude a weaved property from an entity in Spring Data?
- 2D affine mapping using scipy
- In Mapstruct, how to ignore null objects and fields
- org.hibernate.persister.entity.SingleTableEntityPersister
- Mapping unnormalized database into graph
- Changes not saving in Python and Excel
- Map non-boolean vector with text vector in Excel
- Performing PACE PIN authentication using C# and nfc card : Chip Authentication with ECDH
- entityListToResponseModelList unable to find mapped target properties, resulting in null results
- Elasticsearch reindexing - missing child mapping
- Is a full reindex required after adding a new field to Opensearch mapping? (Searchkick)
- GIS Layers into Google Maps Search
- Using Mapperly Library for Custom Constructor Mapping C#
Related Questions in COLUMN-SUM
- Generate an m*n matrix with 1 and 0 (No identity)
- Problem with table data sum in FormIO table element
- Get column data sum from FormIO table element
- Column sum changing name of variables in r
- How to Sum Amounts Column Based On Conditions from Categorical Column in R
- How to add two specific columns from a colSums table in r?
- C# How can i sum a column of a DataTable after change the Rowfilter?
- How to sum columns of jquery datatable on search event or filter event
- Sum column based on condition in another columns in R
- Dynamic column sum total when adding a row with JS
- Tcxgriddbtableview - Sum several columns in a certain row
- Stack Based Column Sum in a data frame using R
- Summing a specific vector index
- How to add together certain rows within a column in R by unique IDs?
- Vertical column summation in sas
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
You can use
sumandsumByto achieve this.Following are 3 methods: (1) to calculate the overall matrix sum, (2) to calculate the sum for a given row, and (3) to calculate the sum for a given column:
According to your question, you would like to call
colSum(0). This will extractrow[0](each row's value for index0) and sum these values.EDIT:
Regarding your question in the comment section, following two methods: (1) To calculate all row sums, returned as a list; and (2) To calculate all col sums, returned as a list.
To explain
colSums(which assumes that all rows have the same number of columns): First, you grab your first row (arr[0]), then you get itsindices(i.e., its column numbers0,1, ..., up tonCols), and then youmapthese indices to their respectivecolSum()results. This may not be the most performant way, but it's quite readable.