Tags down


Create matrix from smaller matrices in R

Date : September 17 2020, 06:00 AM
I wish this help you Is there a general function to build a matrix from smaller blocks, i.e. build matrix , Here are some base R solutions. Maybe you can use
code :
M <- rbind(cbind(A,B),cbind(C,D))
u <- list(list(A,B),list(C,D))
M <- do.call(rbind,Map(function(x) do.call(cbind,x),u))
A <- matrix(1:4,nrow = 2)
B <- matrix(1:6,nrow = 2)
C <- matrix(1:6,ncol = 2)
D <- matrix(1:9,nrow = 3)
> M
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    1    3    5
[2,]    2    4    2    4    6
[3,]    1    4    1    4    7
[4,]    2    5    2    5    8
[5,]    3    6    3    6    9

Share : facebook icon twitter icon

Split a matrix into smaller matrices

By : user6718461
Date : March 29 2020, 07:55 AM
hop of those help? I have a matrix of size 20*20. I want to split it into two sub-matrices of size 20*10. If i use the following code: , do the following
code :
f = mat2cell( e, 20, [10 10] );

Create a larger matrix from smaller matrices in numpy

By : Mehdi Majidi
Date : March 29 2020, 07:55 AM
hop of those help? I have 3 matrices A,B,C. I wish to create a larger matrix of the form , This:
code :
numpy.bmat([[numpy.zeros(appropriate_shape), A], [B, C]])

Algorithm to create a square matrix given any number of smaller square matrices

By : rng
Date : March 29 2020, 07:55 AM
it helps some times I will want to plot some images using Opencv, and for this I would like to glue images together. , Here's a snippet that I use for doing this sort of thing:
code :
import numpy as np

def montage(imgarray, nrows=None, border=5, border_val=np.nan):
    Returns an array of regularly spaced images in a regular grid, separated
    by a border

        3D array of 2D images (n_images, rows, cols)
        the number of rows of images in the output array. if 
        unspecified, nrows = ceil(sqrt(n_images))
        the border size separating images (px)
        the value of the border regions of the output array (np.nan
        renders as transparent with imshow)

    dims = (imgarray.shape[0], imgarray.shape[1]+2*border,
        imgarray.shape[2] + 2*border)

    X = np.ones(dims, dtype=imgarray.dtype) * border_val
    X[:,border:-border,border:-border] = imgarray

    # array dims should be [imageno,r,c]
    count, m, n = X.shape

    if nrows != None:
        mm = nrows
        nn = int(np.ceil(count/nrows))
        mm = int(np.ceil(np.sqrt(count)))
        nn = mm

    M = np.ones((nn * n, mm * m)) * np.nan

    image_id = 0
    for j in xrange(mm):
        for k in xrange(nn):
            if image_id >= count: 
            sliceM, sliceN = j * m, k * n
            img = X[image_id,:, :].T
            M[sliceN:(sliceN + n), sliceM:(sliceM + m)] = img
            image_id += 1

    return np.flipud(np.rot90(M))
from scipy.misc import lena
from matplotlib import pyplot as plt

img = lena().astype(np.float32)
img -= img.min()
img /= img.max()
imgarray = np.sin(np.linspace(0, 2*np.pi, 25)[:, None, None] + img)

m = montage(imgarray)
plt.imshow(m, cmap=plt.cm.jet)

How can I create a matrix based on smaller matrices?

By : Giuseppe Diciolla
Date : March 29 2020, 07:55 AM
it should still fix some issue How about http://docs.scipy.org/doc/scipy-dev/reference/generated/scipy.sparse.vstack.html
If that's too slow, take the fast path from here: https://github.com/scipy/scipy/blob/master/scipy/sparse/construct.py#L396 (in future Scipy versions, vstack itself will be fast in this case).

Numpy: create a matrix from smaller matrices

By : user33362
Date : March 29 2020, 07:55 AM
will be helpful for those in need We can use NumPy's Kronecker product -
code :
np.kron(np.ones((2, 2), dtype=int), m)
In [140]: m
array([[1, 2],
       [3, 4]])

In [141]: np.kron(np.ones((2, 2), dtype=int), m)
array([[1, 2, 1, 2],
       [3, 4, 3, 4],
       [1, 2, 1, 2],
       [3, 4, 3, 4]])
Related Posts Related Posts :
  • 45 degree line and same coordinate lengths in scatter plot in R
  • Mapping multiple columns in R including a single column containing comma-separated values
  • How to unscale Neural Network Predictions
  • R : Merge several dataframes from a list into a single dataframe with differentiation between dataframes
  • How to remove value to the front of the data.frame in R
  • How to print the corresponding ID to the max() value
  • Converting a column of integers that aren't in date format already into abbreviated months
  • Coerce column values to column names and condense repeat observations in R
  • How to replace values in the columns of a dataframe based on the values in the other column in R?
  • What can't I do with dtplyr that I can in data.table
  • How to replace NA value with previous non NA value given to same ID
  • Using Regex to find the characters '!' and '?' in a string
  • R : How to join several lists of a single element into a single list of an element
  • Calculating an average using tidyverse
  • Calculating category specific variable in R
  • lm()-function gives different results on SolusOS Linux than on Windows
  • Mark a period of time in a time-series-plot
  • how to execute function over the data frame
  • Add unique rows to dataframe (opposite of intersect)
  • Convert vector element in tibble to expand
  • Storing lists with different values inside a for loop R
  • Pasting 2 columns to a new variable in a ordered format
  • R - How to collapse rows of dataframe taking the max of each column for each unique id
  • How to render ggplot2+gganimate+ggflags faster
  • R and ggplot: shade the color of the categorical variable
  • How to return a server object in Ui?
  • How can I efficiently group several columns into one list column in data.table
  • Working with transform and mutate in tidyverse to calculate a cumulative sum
  • Sort a vector using a "personalised" order in R
  • Can we log individual user details in R Shiny if i host my application from my pc as server?
  • subtract a different vector from each row in a matrix in r
  • Why is data.table slower than base R for these lookup table-style queries?
  • How to customize the content of a legend of a dotplot with ggplot2
  • What is wrong w/ this boxplot?
  • Merge 2 csv files into one with different columns in R
  • How to avoid loop in *multiplication of multiple rows in data frame
  • Add lateral facets to a dotplot with multiple values for variables
  • Looping over multiple data frames to ggplot
  • Using a for loop to create new datasets in the environment
  • What's the shortest code to generate the below matrices in R?
  • split the superhero names with their ages in the given vector
  • Excluding NA and odd number
  • R: how to assign seasons to months (factor)
  • R: plotting sky map in Mollweide projection in galactic coordinate system
  • Split rows to columns in R
  • Mutate with column value >0
  • Equivalent function to excel's replace
  • R: Unusual error plotting multipolygons with ggplot, geom_sf, and openstreetmap
  • group by text in columns, look for common entries in two data frames
  • How can I remove NAs when both columns are missing only?
  • Fill up a list using an existing vector and a double for loop
  • Count frequency of events in R
  • Replace value in dataframe by value divided by mean of the entire column
  • How to update selected rows in a data.table if they match value from another data.table
  • concatenate iteratively first/last n words from strings
  • Form a vector A of length 100 whose odd and even position value will be 2 and 3, respectively
  • How to create a Qrcode list of names in r
  • How to subset a dataframe by unique character in R?
  • How to select a nested field with bigrquery using dplyr syntax?
  • look for common entries in two data frames
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk