File Exchange

image thumbnail

InplaceArray: a semi-pointer package for Matlab

version (10.9 KB) by Bruno Luong
Manipulate (multi-dimensional) arrays in the in-place form

1 Download

Updated 18 Jul 2012

View Version History

View License

This package helps to create "children" arrays that share the same data as the mother array. The children array can be shaped any customized size for later manipulation.

In short it provides some sort of equivalent C-"pointer" where Matlab lacks.

Extreme cautious when using this package, otherwise encountered crashes and memory leaks warranty. For "advanced" users only.

SPARSE, STRUCTURE are currently not supported

ATTENTION: Serious bug in testinplace.m in the first submission. Before the new fixed version is submitted please do not run this function (the rest is OK)

Cite As

Bruno Luong (2021). InplaceArray: a semi-pointer package for Matlab (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (9)

Binlin Wu

Josh, thank you for your emails. The code is working now for vectors. It's not critical if it works for scalar or not.

This is a great program and will be very helpful.


Hi Bruno. Can I suggest adding the line:

((Internal_mxArray*)(BMAT))->reserved3 = 1; /*give it a reference count*/

Around about line 194 of inplacearray.c. This will tell Matlab not to try to free the memory pointer so:

A = eye(3);
B = inplacecolumn(A, 2);
clear B;

will not cause Matlab to crash (R2009A anyway)
--Cheers, Andrew

Joshua Dillon

Hi Bruno. I confirm this problem for my sharedmatrix submission. It would seem Mathworks has dropped support for array_access_inlining. Sadly, this is probably the beginning of the end for our hacks. Please let me know if you find a method to automatically reverse engineer future implementations of the mxArray object (and I will do the same). --Cheers, Josh

Bruno Luong

Edit: save the following in Internal_mxArray.h before building the mex file:

Matlab version: 2010B

typedef struct {
void *reserved;
int reserved1[2];
void *reserved2;
size_t number_of_dims;
unsigned int reserved3;
struct {
unsigned int flag0 : 1;
unsigned int flag1 : 1;
unsigned int flag2 : 1;
unsigned int flag3 : 1;
unsigned int flag4 : 1;
unsigned int flag5 : 1;
unsigned int flag6 : 1;
unsigned int flag7 : 1;
unsigned int flag7a: 1;
unsigned int flag8 : 1;
unsigned int flag9 : 1;
unsigned int flag10 : 1;
unsigned int flag11 : 4;
unsigned int flag12 : 8;
unsigned int flag13 : 8;
} flags;
size_t reserved4[2];
union {
struct {
void *pdata;
void *pimag_data;
void *reserved5;
size_t reserved6[3];
} number_array;
} data;
} Internal_mxArray;

Bruno Luong

There is a compilation issue with 2010B, and I still not able to find a good fix. A workaround is compile by hand the mex files:

% On 32-bit platform
mex inplacecolumnmex.c
mex releaseinplace.c

or on 64-bit platform
mex -largeArrayDims inplacecolumnmex.c
mex -largeArrayDims releaseinplace.c

Joshua Dillon

I have figured out how to work with sparse matrices. Contact me if interested.

Joshua Dillon

Wow this really is a gem!

Any ideas how to handle sparse matrices? ...nothing in matrix.h sadly.

Bruno Luong

Hi Christophe,
Thank you for report this. It is expected that PACK would mess up with memory pointers.

Christophe Lauwerys

Nice, however, very tricky.
For instance PACK messes up the reference and I have the feeling more things can go wrong when you use memory-related function calls, or am I being too pessimistic?

>> A=1
>> B=inPlaceArray(A);
>> B=2;
>> pack
>> A
A =

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!