Define 2D array with C++ MATLAB API - c++

I'm using the MATLAB/C++ API.
I'm trying to create a 2D MATLAB array from a 2D C++ array. Here's what I've tried:
double testCppArr[243][5];
// Fill the testCppArr with data
mwArray *testMatlabArr = new mwArray(243, 5, mxDOUBLE_CLASS);
testMatlabArr->SetData(testCppArr[0], 243 * 5);
And that's created for me vector with length of 243×5, instead of a matrix with 243 rows and 5 columns. I know that's the same in memory, but I want to create a matrix and not a vector.
Where is the mistake?
I've also read the MATLAB documentation, but didn't find a solution

The code in the question define a 2D MATLAB matrix.
My problem was in the matrix data read.

Related

Optimal way to append to numpy array when dealing with large dimensions

I am working with a json file that consists of approximately 17,000 3x1 arrays denoting the coordinates.
Currently,I have an image of 1024x1024 dimensions(which I have flattend),and I am using np.hstack to add the 3x1 array to that image ,this gives me a 1d array of dimension 1048579x1
My objective is to create a final array of dimension 1048579x17,000.
Unfortunately list.append and np.append are not working in this case,because it's consuming too much memory.I tried running this on colab pro,but the memory consumption is too high which causes the session to crash
My current code is as follows
image=cv2.imread('image_name.jpg',0)
shape=image.shape
flat_img=image.ravel()
print(flat_img.shape)
#Here data consists of 17,000 entries each of which is a 3x1 list
with open('data.json') as f:
json1_str = f.read()
json1_data=json.loads(json1_str)
local_coordinates=[]
for i in range(len(json1_data)):
local_coord=json1_data[i]['local_coordinate']
local_coord=np.array(local_coord)
new_arr=np.hstack((flat_img,local_coord))
new_arr=new_arr.tolist()
local_coordinates.append(new_arr) #
Is there an optimal way to stack all the 1048579 1d arrays to create the final matrix which can be used for training purposes?

Scilab Image Processing Toolboxes .How to pass 3 dimensional matrix from C++ code to scilab console?

I am working on implementing Image Processing Toolbox in Scilab .For that i am implementing Opencv functions in Scilab through C++ using functions defined in Scilab API "api_scilab.h". Problem which i am facing is that i am unable to return 3 Dimensional Matrix (which is returned by opencv function) through my C++ code to Scilab console.Solution which i found for this problem is that i am converting 3 Dimensional Matrix to 1 Dimensional Matrix and then return it and convert 1 Dimensional Matrix back to 3 Dimensional Matrix whenever required.
I want to ask is there any function in Scilab API "api_scilab.h" by which i can directly return or pass 3 Dimensional matrix as argument .
Thank you in advance

How to reshape 4d array to 2d array in numpy

I have a 4d array of shape like this. It has total 18*100 = 1800 rows and 30 dimensional outputs per row
(18, 100, 30, 1, 1)
i want to convert or reshape this into 2d array, the easiest way
(1800,30)
Sorry for being so naive with numpy, but please i am a novice user. Any help much appreciated.
numpy.reshape(input_in_4D, (1800,30))
Of course this just converts the input in the default order (meaning you "unroll" the input array from inner to outer); if you need special ordering, you should read up on slicing.
You can use reshape method:
newArray = oldArray.reshape(1800,30)

send row and column of a matrix, MPI_SEND

I can send the row of a matrix, and the column of another matrix in a single MPI_SEND, how can I do this procedure?
MPI_SEND (row and column of the matrix ...)
Since C/C++ works in row major order, you can either declare your 2D matrix in a 1D vector and access its entries by using a simple convention. Assuming A is of size mxn and B is a vector;
A[i][j] = B[i*n+j];
But if you are coding in C++, I would suggest defining a matrix object and try sending these objects in MPI. You would have to create your own MPI_Data_Type.

C++ Submatrix from a Multidimensional Vector/Matrix

In MSVS C++ I have a multidimensional vector (matrix). I am not using arrays.
For example:
vector< vector<float> > image(1056, vector<float>(366));
After data is included in the vector from another source how is it possible to create a sub matrix from this matrix, given an pixel co-ordinate and the number of columns and rows needed?
For example, I have:
1 2 3 4
5 6 7 8
9 10 11 12
I want:
6 7
10 11
Seems basic but I am new to this concept. There are examples but they use arrays and I was unable to change the samples around for my own need.
There is no simple way to do it. You should create new two-dimensional array of desired size and copy pieces of data to it.
You may want to access matrix through some view, which would be proxy class, mapping view indices, to underlaying data indices