matrix

【Leetcode】240. Search a 2D Matrix II

拟墨画扇 提交于 2020-01-12 20:07:41
题目地址: https://leetcode.com/problems/search-a-2d-matrix-ii/ 给定一个矩阵,满足每行都是单调增的,每列也是单调增的。另给定一个数,问该矩阵是否包含这个数。基本思路是,选定矩阵最左下角的那个数 a a a ,如果 t a r g e t target t a r g e t 比 a a a 大,由于 a a a 所在列在 a a a 上方的数都比 a a a 小,所以不用再搜索,那就向 a a a 的右边走一步;如果 t a r g e t target t a r g e t 比 a a a 小,由于 a a a 所在行在 a a a 右边的数都比 a a a 大,所以不用再搜索,那就向 a a a 的上边走一步。如此这般,直到整个矩阵搜索完毕。 例如,我们要在下面的矩阵里搜索 5 5 5 : [ 1 4 7 11 15 2 5 8 12 19 3 6 9 16 22 10 13 14 17 24 18 21 23 26 30 ] \begin{bmatrix} 1 & 4 & 7 & 11 & 15\\ 2 & \textbf{5} & 8 & 12 & 19\\ \textbf{3} & \textbf{6} & 9 & 16 & 22\\ \textbf{10} & 13 & 14 & 17 & 24\\ \textbf

How to interpolate rotations?

浪尽此生 提交于 2020-01-12 07:29:07
问题 I have two vectors describing rotations; a start rotation A and a target rotation B. How would I best go about interpolating A by a factor F to approach B? Using a simple lerp on the vectors fails to work when more than one dimension needs to be interpolated (i.e. produces undesirable rotations). Maybe building quaternions from the rotation vectors and using slerp is the way to go. But how, then, could I extract a vector describing the new rotation from the resulting quaternion? Thanks in

How can I initialize a cv::Mat with data from a float array

强颜欢笑 提交于 2020-01-12 06:46:50
问题 I need to create a cv::Mat variable that is initialized with my data from a float * array. This should be basic, but I'm having trouble figuring it out. I have the code: float *matrixAB = <120 floating point array created elsewhere>; cv::Mat cv_matrixAB = cv::Mat(12, 10, CV_32F, &matrixAB); but cv_matrixAB never contains float values, and more importantly doesn't match the data contained in matrixAB . If I change the line to: cv::Mat cv_matrixAB = cv::Mat(12, 10, CV_32F, matrixAB); then the

Pairwise interaction matrix in R

白昼怎懂夜的黑 提交于 2020-01-12 03:52:08
问题 I am trying to compute a pairwise matrix in R that counts the number of times individuals interact with other individuals (so the matrix will include N number of rows and columns corresponding to number of individuals). I have a dataframe that lists "actors" and "partners" in separate columns. nn <- data.frame(actors=c('DOL','DOL','DOL','DOL','DOL','NOR','NOR','NOR','NIN','JOJ'),partners=c('JOJ','JOJ','NOR','NOR','NIN','NIN','DOL','JOJ','NOR','NOR')) The data are such that direction of the

display matrix values and colormap

别等时光非礼了梦想. 提交于 2020-01-12 03:14:07
问题 I need to display values of my matrix using matshow. However, with the code I have now I just get two matrices - one with values and other colored. How do I impose them? Thanks :) import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots() min_val, max_val = 0, 15 for i in xrange(15): for j in xrange(15): c = intersection_matrix[i][j] ax.text(i+0.5, j+0.5, str(c), va='center', ha='center') plt.matshow(intersection_matrix, cmap=plt.cm.Blues) ax.set_xlim(min_val, max_val) ax.set

Handling large dense matrices in python

自作多情 提交于 2020-01-12 02:38:11
问题 Basically, what is the best way to go about storing and using dense matrices in python? I have a project that generates similarity metrics between every item in an array. Each item is a custom class, and stores a pointer to the other class and a number representing it's "closeness" to that class. Right now, it works brilliantly up to about ~8000 items, after which it fails with a out-of-memory error. Basically, if you assume that each comparison uses ~30 (seems accurate based on testing)

【d3.js】弦图

牧云@^-^@ 提交于 2020-01-11 22:26:51
d3-弦图 弦图主要用来展示节点之间的联系。主要包含两部分,节点部分和表示节点联系的弦部分,弦的粗细表示比重。 使用场景: 1.城市之间的人口流动比例。 2.一群人的邮件交流比例。 … 画一个弦图,首先需要一个矩阵数据 matrix ,以二位数组表示,数组每一项都是一个节点,而 matrix[i][j] 表示第i个节点到第j个节点的流量。 const matrix = [ [ 10 , 20 , 20 , 30 , 40 ] , // 节点1 [ 5 , 15 , 55 , 10 , 10 ] , // 节点2 [ 20 , 5 , 80 , 5 , 5 ] , // ... [ 10 , 20 , 20 , 30 , 40 ] , ] 然后使用 d3.chord 处理数据 const chords = d3 . chord ( ) ( matrix ) 返回值是一组 chords ,表示一对节点间的流量,包含两个属性: source:弦的源节点对象 target:弦的目标节点对象 source和target 对象都包含下列的属性: startAngle 起始角度 endAngle 终止角度 index 索引i subindex 索引j value matrix[i][j] 的值 然后使用 d3.ibbon 来绘制所有的弦: const colors = d3 .

super fast median of matrix in opencv (as fast as matlab)

最后都变了- 提交于 2020-01-11 18:11:08
问题 I'm writing some code in openCV and want to find the median value of a very large matrix array (single channel grayscale, float). I tried several methods such as sorting the array (using std::sort) and picking the middle entry but it is extremely slow when comparing with the median function in matlab. To be precise - what takes 0.25 seconds in matlab takes over 19 seconds in openCV. My input image is originally a 12-bit greyscale image with the dimensions 3840x2748 (~10.5 megapixels),

Matrix multiplication in php

孤者浪人 提交于 2020-01-11 13:55:15
问题 Although the order of matrices should be fine, the following code throws back the exception. It might be a tiny thing I'm not being able to notice, but can't figure it out. <?php $mat1 = array(5,1); $mat2 = array(1,5); function matrixmult($m1,$m2){ $r=count($m1); $c=count($m2[0]); $p=count($m2); if(count($m1[0])!=$p){throw new Exception('Incompatible matrixes');} $m3=array(); for ($i=0;$i< $r;$i++){ for($j=0;$j<$c;$j++){ $m3[$i][$j]=0; for($k=0;$k<$p;$k++){ $m3[$i][$j]+=$m1[$i][$k]*$m2[$k][$j

how to generate a query in SQL SERVER 2005 which gives output like Matrix?

流过昼夜 提交于 2020-01-11 13:43:07
问题 H i have column which consists of 32 rows. like ColumnA 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 while retrieving i want (4 X 8) means 4 columns 8 Rows.The Result should be like this A B C D 1 9 17 25 2 10 18 26 3 11 19 27 4 12 20 28 5 13 21 29 6 14 22 30 7 15 23 31 8 16 24 32 Give me an Idea. 回答1: Something like this using CTE and row_number() : Fiddle demo declare @numRows int = 8 ;with cte as ( select columnA X, row_number() over (order by