logo
down
shadow

How to optimize intersect of rows and columns in a matrix?


How to optimize intersect of rows and columns in a matrix?

By : user3100006
Date : January 11 2021, 05:14 PM
this will help I wrote a coded_best_intersect function that relies on creating a for loop dynamically in a code_maker function. It evaluates M3 in 30 seconds. Because the code generates a list, I am depending on data.table for rbindlist and the print method.
code :
library(data.table)
code_maker <- function(non_NA_M, n, k, min.col) {
  ## initializing for results
  res <- list()
  z <- 1
  ## initializing naming
  col_names <- colnames(non_NA_M)
  i_s <- paste0('i', seq_len(k))
  ## create the foor loop text. It looks like this mostly
  ## for (i1 in 1:(n - k + 1)) { for (i2 in (i1 + 1):(n-k+2)) {}}
  for_loop <- paste0('for (', i_s, ' in ', c('1:', paste0('(', i_s[-k], ' + 1):')), 
                     n - k + seq_len(k), ')', ' {\n non_na_sums', seq_len(k), 
                     '=non_NA_M[', i_s, ', ] ',
                     c('', paste0('& ', rep('non_na_sums', k - 1), seq_len(k)[-k])), '', 
                     '\n if (sum(non_na_sums', seq_len(k), ') < ', min.col, ') {next} ', 
                     collapse='\n')
  ## create the assignment back to the results which looks like
  ## res[[z]] <- data.table(M=k, N=sum(non_na_sumsk), ROWS=list(c(i1, i2, ..., ik)), 
  ##                        YEARS=list(col_names[non_na_sumsk]))
  inner_text <- paste0('\nres[[z]] <- data.table(M=k, N=sum(non_na_sums',
                       k, '), ROWS=list(c( ', paste0(i_s, collapse=', '), 
                       ')), YEARS=list(col_names[non_na_sums', k , ']))\nz <- z + 1')
  ## combines the loop parts and closes the for with }}}
  for_loop <- paste(for_loop, 
                    inner_text, 
                    paste0(rep('}', k), collapse=''))
  ## evaluate - the evaluation will assign back to res[[i]]  
  eval(parse(text=for_loop))
  res <- rbindlist(res)
  if (length(res) == 0) { #to return emtpy data.table with the correct fields
    return(data.table(M=integer(), N=integer(), ROWS=list(), YEARS=list()))
  }
  res$M <- k
  return(res)
}
coded_best_intersect <- function(M, min.row=5, min.col=3) {
  colnames(M) <- apply(M, 2, function(x) na.omit(x)[1])
  n_row <- nrow(M)
  non_NA <- !is.na(M)
  n_combos <- min.row:(n_row - 1)
  res2 <- list()
  for (i in seq_along(n_combos)) {
    res2[[i]] <- code_maker(non_NA, n=n_row, k=n_combos[i], min.col)
    if (nrow(res2[[i]]) == 0) {
      break
    }
  }
  return(res2)
}
# for (i1 in 1:5) {
#   non_na_sums1=non_NA_M[i1, ] 
#   if (sum(non_na_sums1) < 3) {next} 
#   for (i2 in (i1 + 1):6) {
#     non_na_sums2=non_NA_M[i2, ] & non_na_sums1
#     if (sum(non_na_sums2) < 3) {next} 
#     for (i3 in (i2 + 1):7) {
#       non_na_sums3=non_NA_M[i3, ] & non_na_sums2
#       if (sum(non_na_sums3) < 3) {next} 
#       for (i4 in (i3 + 1):8) {
#         non_na_sums4=non_NA_M[i4, ] & non_na_sums3
#         if (sum(non_na_sums4) < 3) {next} 
#         for (i5 in (i4 + 1):9) {
#           non_na_sums5=non_NA_M[i5, ] & non_na_sums4
#           if (sum(non_na_sums5) < 3) {next} 
#           for (i6 in (i5 + 1):10) {
#             non_na_sums6=non_NA_M[i6, ] & non_na_sums5
#             if (sum(non_na_sums6) < 3) {next}  
#             res[[z]] <- data.table(M=k, N=sum(non_na_sums6), 
#                                    ROWS=list(c( i1, i2, i3, i4, i5, i6)),
#                                    YEARS=list(col_names[non_na_sums6]))
#             z <- z + 1 }}}}}}
system.time(final1 <- coded_best_intersect(M1))
   user  system elapsed 
      0       0       0 
data.table::rbindlist(final1)[order(-M*N)]
   M N           ROWS          YEARS
1: 5 3  2, 4, 8, 9,10 2002,2004,2010
system.time(final2 <- coded_best_intersect(M2))
   user  system elapsed 
   0.08    0.00    0.08 
data.table::rbindlist(final2)[order(-M*N)]
     M N                  ROWS               YEARS
  1: 7 3  6, 8,11,12,13,16,...      2002,2012,2013
  2: 5 4         6, 8,13,16,17 2002,2012,2013,2015
  3: 5 4         8,11,12,13,17 2002,2012,2013,2014
  4: 6 3      1, 4, 8,13,17,20      2002,2014,2015
  5: 6 3      2, 5, 6,10,14,17      2003,2006,2008
 ---                                              
126: 5 3        10,12,13,17,20      2002,2008,2014
127: 5 3        10,12,14,17,20      2003,2008,2014
128: 5 3        11,12,13,16,17      2002,2012,2013
129: 5 3        11,12,13,17,20      2002,2012,2014
130: 5 3        12,13,15,16,19      2001,2002,2013
system.time(final3 <- coded_best_intersect(M3))
   user  system elapsed 
  29.37    0.05   29.54 
data.table::rbindlist(final3)[order(-M*N)]
       M N              ROWS                             YEARS
    1: 6 7  1, 3, 8,15,20,29 1969,1973,1980,1984,1985,1992,...
    2: 5 8     1, 3, 8,14,29 1969,1973,1976,1980,1984,1987,...
    3: 5 8     1, 3, 8,20,29 1969,1973,1980,1984,1985,1992,...
    4: 5 8     2, 7, 9,13,17 1974,1993,1994,2004,2012,2013,...
    5: 5 8     3, 6, 8, 9,27 1974,1980,1984,1987,1995,1998,...
   ---                                                        
52374: 5 3    23,24,25,30,31                    1979,1997,2002
52375: 5 3    23,25,28,30,31                    1979,1992,2002
52376: 5 3    24,25,26,30,31                    1983,1997,2002
52377: 5 3    24,25,28,30,31                    1979,1983,2002
52378: 5 3    24,26,28,30,31                    1983,1986,2002
x <- data.table::rbindlist(final3)[order(-M*N)]
el(x$YEARS[1])  # select `YEARS` of result-row `1:`
# [1] "1969" "1973" "1980" "1984" "1985" "1992" "2003"


Share : facebook icon twitter icon
How to find intersect rows when condition depend on some columns in one table

How to find intersect rows when condition depend on some columns in one table


By : Kuzey Kara
Date : March 29 2020, 07:55 AM
may help you . Table subscribe
code :
DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(subscriber INT NOT NULL
,subscribeto INT NOT NULL
,PRIMARY KEY(subscriber,subscribeto)
);

INSERT INTO my_table VALUES
(1,5),
(1,6),
(1,7),
(1,8),
(1,9),
(1,10),
(2,5),
(2,6),
(2,7);

SELECT x.subscribeto mutual_friends 
  FROM my_table x 
  JOIN my_table y 
    ON y.subscribeto = x.subscribeto 
 WHERE x.subscriber = 1 
   AND y.subscriber = 2;
+----------------+
| mutual_friends |
+----------------+
|              5 |
|              6 |
|              7 |
+----------------+
How to get a 2 columns, 8 rows matrix instead of returning 2 rows, 8 columns matrix using sapply?

How to get a 2 columns, 8 rows matrix instead of returning 2 rows, 8 columns matrix using sapply?


By : Jakoma02
Date : March 29 2020, 07:55 AM
wish of those help The default for sapply is to essentially cbind the the final output. You can either tell it to not simplify or just transpose your result.
code :
# manual rbind
do.call("rbind", sapply(1:8, function(x) c(x,x^2), simplify=FALSE))

# transpose result
t(sapply(1:8, function(x) c(x,x^2)))     
Optimize matrix to have the least number of rows with NaN

Optimize matrix to have the least number of rows with NaN


By : Maryam Yazdandoost
Date : March 29 2020, 07:55 AM
around this issue In order to continue removing NaNs from your matrix, you need to have some rule for how to maximize the tradeoff between less data and less NaN. As you said, if you continue to remove NaNs without any limit - you may remain with a very small amount of data. There is no correct rule for that, it really depends on what you are asking, the following suggestion is only to give you an idea of how to deal with such a problem.
So as a starting point, I define an index to the 'quality' of the matrix, in terms of how many 'holes' are in it:
code :
M_ratio = sum(~isnan(M(:)))/numel(M); % the ratio between numbers to M size
improve = 1-M_old_ratio/M_new_ratio % the relative improvement after deletion
N = 100;
M = rand(N); % generate a NxN random matrix
M(randi(numel(M),N^2,1)) = nan;  % add NaN to randomly selected N^2 measurements
M(:,all(isnan(M)))=[]; % delete all NaN columns
M(all(isnan(M),2),:)=[]; % delete all NaN rows
threshold = 0.003; % the threshold for stop optimizing the matrix
while 1
    M_ratio = sum(~isnan(M(:)))/numel(M); % the ratio between numbers to M size
    [mincol,indcol] = min(sum(~isnan(M),1)); % find the column with most NaN
    [minrow,indrow] = min(sum(~isnan(M),2)); % find the row with most NaN
    [~,dir] = min([minrow;mincol]); % find which has more NaNs
    Mtry = M;
    if dir == 1
        Mtry(indrow,:) = []; % delete row
    else
        Mtry(:,indcol) = []; % delete column
    end
    Mtry_ratio = sum(~isnan(Mtry(:)))/numel(Mtry); % get the new ratio
    improve = 1-M_ratio/Mtry_ratio; % the relative improvement after deletion
    if improve>threshold % if it improves more than the threshold
        M = Mtry; % replace the matrix
    else
        break; % otherwise - quit
    end
end
threshold = 0.002; % the threshold for stop optimizing the matrix
while 1
    M_ratio = sum(~isnan(M(:)))/numel(M); % the ratio between numbers to M size
    [~,indcol] = min(sum(~isnan(M),1)); % find the column with most NaN
    Mtry = M;
    Mtry(:,indcol) = []; % delete column
    Mtry_ratio = sum(~isnan(Mtry(:)))/numel(Mtry); % get the new ratio
    improve = 1-M_ratio/Mtry_ratio; % the relative improvement after deletion
    if improve>threshold % if it improves more than the threshold
        M = Mtry; % replace the matrix
    else
        break; % otherwise - quit
    end
end
Algorithm that determines if a given binary matrix can be reached by flipping rows and columns of a matrix of ones

Algorithm that determines if a given binary matrix can be reached by flipping rows and columns of a matrix of ones


By : hagiwara halley
Date : March 29 2020, 07:55 AM
wish of those help This can be tested as follows:
Take the first column of the target matrix. All other columns should be either the same as that first column, or should be its opposite (flipped). If, and only when, this is the case then the target matrix can be reached through flips of rows/columns from the initial matrix having all 1 values.
code :
function getFlips(matrix) {
    // Verification
    for (let i = 1; i < matrix.length; i++) {
        let flip = matrix[i][0] ^ matrix[0][0]; // XOR operation
        for (let j = 0; j < matrix[0].length; j++) {
            if (matrix[i][j] ^ flip != matrix[0][j]) return false; // Not possible
        }
    }
    // If we get here, it is possible: determine which rows/columns to flip
    let flips = { rows: [], columns: [] };
    for (let j = 0; j < matrix[0].length; j++) {
        if (matrix[0][j] == 0) flips.columns.push(j+1);
    }
    for (let i = 1; i < matrix.length; i++) {
        if (matrix[i][0] != matrix[0][0]) flips.rows.push(i+1);
    }
    return flips;
}


// I/O management
inp.oninput = function () {
    // Convert input to matrix of numbers
    let matrix = inp.value.split('\n').map(row => Array.from(row, Number));
    // Perform algorithm
    let flips = getFlips(matrix);
    // Output the result in human readable format
    out.textContent = flips 
        ? 'Number(s) of the column(s) to flip: ' 
            + (flips.columns.length ? flips.columns : 'none') + '\n' +
          'Number(s) of the row(s) to flip: ' 
            + (flips.rows.length ? flips.rows : 'none')
        : 'Not possible';
};

inp.oninput();
Enter the values of the matrix:<br>
<textarea id="inp" rows="4" cols="4">101
010
101</textarea><br>
Solution:
<pre id="out"></pre>
Construct single column matrix from the rows of another matrix with multiple columns in Matlab

Construct single column matrix from the rows of another matrix with multiple columns in Matlab


By : user2035445
Date : March 29 2020, 07:55 AM
Hope this helps I'm learning about image processing, and currently translating between RGB and YCbCr. , If you have an RGB image, Imgrgb that is NxMx3 in size, do:
code :
Imgrgb= imread('peppers.png');  % A sample RGB image
% make sure you have a double image range [0-1], you may not need this.
Imgrgb=im2double(Imgrgb);

r=Imgrgb(:,:,1);
g=Imgrgb(:,:,2);
b=Imgrgb(:,:,3);

y = (0.299 * r) + (0.587 * g) + (0.114 * b);

Cb=
Cr=

% concatenate result:
ImgYCbCr=cat(3,y,Cb,Cr);
Related Posts Related Posts :
  • How to retrieve the data frame used in a GEE model fit?
  • R: How to find/replace and then automatically execute code?
  • Slope of time series (xts) object over rolling window
  • Is there an R function for comparing rows in data.frame?
  • Changing linetype and line color with plot_model()
  • Update existing package on CRAN
  • Delete NA data ,but with certain condition in R
  • calculate number and names of similar sounding words from a data frame
  • Reset input fields of dynamically generated widgets through insertUI
  • Select certain region of column for lm
  • Convert multiple rows into one row depending on unique values in another column
  • Issues installing Plotly Dash for R
  • Is there an R function to retrieve values from a matrix of column names?
  • R;Too slow to overate loops for million vectors
  • Format and export the output of Mann-Kendall test in R to excel from Rstudio
  • how to calculate cumsum with depreciation in a grouped dataframe?
  • reshape wide to long based on part of column name
  • How to get a hyperlink for the words in a description in an r dataframe?
  • shinymeta works locally but breaks when published to shinyapps.io
  • Deparse and (un)escape quotes
  • Regression table with clustered standard errors in R jupyter notebook?
  • Disaggregate quarterly data to daily data in R keeping values?
  • How to save output to console and file simultaneously in RStudio server?
  • Why does data.table j have a different environment when directly calling mget() vs calling mget() inside another functio
  • scale_fill_viridis_c color bar on a log scale
  • How to change the lab name corresponding to function in ggplot
  • R, filtering for an element in a list in a dataframe cell
  • Extracting only bottom temperature from 4d NetCDF file
  • How to add/wrap lines of text to .tex with .sh script
  • R - building new variables from sequenced data
  • Sum rows values one after the other
  • Nesting ifelse inside summarytools
  • How best to divide different levels of a factor by one another in dataframe in R?
  • Why does my code run multiple times before I type data into the table? How do I make an action button that creates a tab
  • How to impute missing values not at random?
  • Set the y limits of an added average line of a plotly plot
  • how to calculate a new column after grouping with dplyr
  • Extract data from rows creating new columns using R
  • Create a filled area line plot with plotly
  • When do I need parentheses around an if statement to control the sequence of a formula in R?
  • my graph in ggplot2 contains an "e" character in y-axis
  • Making variables immutable in R
  • R: Difference between the subsequent ranks of a item group by date
  • Match data within multiple time-frames with dplyr
  • Conditional manipulation and extension of rows in data.table also considering previous extensions without for-loop
  • Conditional formula referring to preview row in DF not working
  • Set hoverinfo text in plotly scatterplot
  • Histogram of Sums from Categorical/Binary Data
  • Efficiently find set differences and generate random sample
  • Find closest points from data set B to point in data set A, using lat long in R
  • dplyr join on column A OR column B
  • Replace all string if row starts with (within a column)
  • Is there a possibility to combine position_stack and nudge_x in a stacked bar chart in ggplot2?
  • How can I extract bounding boxes in a row-wise manner using R?
  • How do I easily sum up values in different columns?
  • How to identify all columns that contain binary representation
  • Filter different groups by different factor levels
  • Saving .xlsx file to disc, form http post request
  • Add an "all" option under the filter that selects the number of rows displayed in a datatable
  • How to select second column of every xts in list
  • shadow
    Privacy Policy - Terms - Contact Us © festivalmusicasacra.org