multidimensional-array

Why is char[][] = {{…}, {…}} not possible if explicitly given a multidimensional array?

一曲冷凌霜 提交于 2020-07-27 05:42:17
问题 I went through this article. I understand the rules explained but I am wondering what exactly blocks the compiler from accepting the following syntax when defining a constant multi-dimensional array and directly initializing it with known values of given type: const int multi_arr1[][] = {{1,2,3}, {1,2,3}}; // why not? const int multi_arr2[][3] = {{1,2,3}, {1,2,3}}; // OK error: declaration of 'multi_arr1' as multidimensional array must have bounds for all dimensions except the first What

How do I concatenate two one-dimensional arrays in NumPy?

爷,独闯天下 提交于 2020-07-22 21:34:03
问题 I have two arrays A = [a1, ..., an] and B = [b1, ..., bn] . I want to get new matrix C that is equal to [[a1, b1], [a2, b2], ... [an, bn]] How can I do it using numpy.concatenate ? 回答1: How about this very simple but fastest solution ? In [73]: a = np.array([0, 1, 2, 3, 4, 5]) In [74]: b = np.array([1, 2, 3, 4, 5, 6]) In [75]: ab = np.array([a, b]) In [76]: c = ab.T In [77]: c Out[77]: array([[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6]]) But, as Divakar pointed out, using np.column_stack

rotate matrix M*N anticlockwise

与世无争的帅哥 提交于 2020-07-22 08:35:51
问题 I am not able to rotate a M*N matrix in anticlockwise direction. My code is working properly for 3*3 matrix but when I try for any other case it is not working assume I am doing it for 4*4 matrix then only outer elements are rotating and inner 4 elements i.e. 6,7,10,11 are not rotating. My input is 1-16 numbers as 4*4 matrix { {1, 2, 3, 4}, {5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 16} } static void antirotatematrix(int m, int n, int mat[][]) { int row = 0, col = n-1; int prev, curr; while

Sorting 2D array in new 2D array (K-menas clustering) Java

ε祈祈猫儿з 提交于 2020-07-22 06:09:41
问题 As input I have 2D array PointXY[ ][ ] clusters, which looks like this: [[23.237633,53.78671], [69.15293,17.138134], [23.558687,45.70517]] . . . [[47.851738,16.525734], [47.802097,16.689285], [47.946404,16.732542]] [[47.89601,16.638218], [47.833263,16.478987], [47.88203,16.45793]] [[47.75438,16.549816], [47.915512,16.506475], [47.768547,16.67624]] . . . So elements in array are of type PointXY[ ], defined like this public PointXY(float x, float y) { this.x = x; this.y = y; } what I would like

Broadcasting a function to a 3D array Python

孤街醉人 提交于 2020-07-21 07:45:02
问题 I tried understanding numpy broadcasting with 3d arrays but I think the OP there is asking something slightly different. I have a 3D numpy array like so - IQ = np.array([ [[1,2], [3,4]], [[5,6], [7,8]] ], dtype = 'float64') The shape of this array is (2,2,2). I want to apply a function to each 1x2 array in this 3D matrix like so - def func(IQ): I = IQ[0] Q = IQ[1] amp = np.power((np.power(I,2) + np.power(Q, 2)),1/2) phase = math.atan(Q/I) return [amp, phase] As you can see, I want to apply my

Broadcasting a function to a 3D array Python

旧时模样 提交于 2020-07-21 07:43:30
问题 I tried understanding numpy broadcasting with 3d arrays but I think the OP there is asking something slightly different. I have a 3D numpy array like so - IQ = np.array([ [[1,2], [3,4]], [[5,6], [7,8]] ], dtype = 'float64') The shape of this array is (2,2,2). I want to apply a function to each 1x2 array in this 3D matrix like so - def func(IQ): I = IQ[0] Q = IQ[1] amp = np.power((np.power(I,2) + np.power(Q, 2)),1/2) phase = math.atan(Q/I) return [amp, phase] As you can see, I want to apply my

Broadcasting a function to a 3D array Python

不羁岁月 提交于 2020-07-21 07:42:08
问题 I tried understanding numpy broadcasting with 3d arrays but I think the OP there is asking something slightly different. I have a 3D numpy array like so - IQ = np.array([ [[1,2], [3,4]], [[5,6], [7,8]] ], dtype = 'float64') The shape of this array is (2,2,2). I want to apply a function to each 1x2 array in this 3D matrix like so - def func(IQ): I = IQ[0] Q = IQ[1] amp = np.power((np.power(I,2) + np.power(Q, 2)),1/2) phase = math.atan(Q/I) return [amp, phase] As you can see, I want to apply my

Remove Parent in PHP Multidimensional Array

孤街醉人 提交于 2020-07-20 08:08:17
问题 What's the best way to remove the parent of a matched key in an Multidimensional Array? For example, let's assume we have the following array and I want to find "[text] = a" and then delete its parent array [0]... (array) Array ( [0] => Array ( [text] => a [height] => 30 ) [1] => Array ( [text] => k [height] => 30 ) ) 回答1: Here’s the obvious: foreach ($array as $key => $item) { if ($item['text'] === 'a') { unset($array[$key]); } } 回答2: using array_filter: function filter_callback($v) { return

What is the syntax for using the restrict keyword for a 2d array function parameter?

喜夏-厌秋 提交于 2020-07-16 08:21:12
问题 I have an array declared in my main function: float A[n][n]; My goal is to pass it to a function with the restrict keyword: void func(int n, float restrict A[][n]) I tried the syntax above, but I am not getting the optimization in running time that I am expecting. I have also seen this syntax for 1d arrays: void func(int n, float A[restrict]) 回答1: The pointer can be restrict. All below forms are equivalent: void func(int n, float A[restrict n][n]); void func(int n, float A[restrict][n]); void

What is the syntax for using the restrict keyword for a 2d array function parameter?

折月煮酒 提交于 2020-07-16 08:21:08
问题 I have an array declared in my main function: float A[n][n]; My goal is to pass it to a function with the restrict keyword: void func(int n, float restrict A[][n]) I tried the syntax above, but I am not getting the optimization in running time that I am expecting. I have also seen this syntax for 1d arrays: void func(int n, float A[restrict]) 回答1: The pointer can be restrict. All below forms are equivalent: void func(int n, float A[restrict n][n]); void func(int n, float A[restrict][n]); void