How to reset a MultiDimensional Array to allow a fresh building of the MDA

2 views (last 30 days)
Hello.
I am trying to find statistical variations between upto 10 matrices. the idea is everytime I run my code via a pushbutton, a file containing a matix is loaded and then added to a multi-dimensional array via the 3rd argument that I call "page". At the end I perform elementwise statistics, but need the option to start again. Hence i have another button to "reset" the MDA. Im therefore declaring my multidimensional array MDA as global.
At the start iof my pushbutton function I have
global MDA % Needs to be global as I have another pushbutton that resets the MDA
[x,y,page]=size(MDA) % I then determine the current size (in the 3rd dimension)
MDA(:,:,page+1)=data % Add the latest data (just read from file)
max1=max(MDA,[],3) % Get elementwise max of the data
min1=min(MDA,[],3) % Get elementwise min of the data
Then in a reset pushbutton callback I have
global MDA;
MDA=[];
But when I do a size on this,
[x,y,page]=size(MDA)
page is 1.
I really need the reset to put page=0 as my MDA would otherwise contains zeros in the first page, unless there is a better way to do this
Thanks
Jason

Accepted Answer

Jon
Jon on 4 Oct 2019
Edited: Jon on 4 Oct 2019
There may be some cleaner way to do this, but I would suggest that one approach would be to use an if statement to test if MDA is empty and if it is start a new one. So somthing like
if ~isempty(MDA)
% normal case, append to existing MDA
[x,y,page]=size(MDA) % I then determine the current size (in the 3rd dimension)
MDA(:,:,page+1)=data % Add the latest data (just read from file)
else
% new multidimensional array
MDA(:,:,1)=data % Add the latest data (just read from file)
end
max1=max(MDA,[],3) % Get elementwise max of the data
min1=min(MDA,[],3)
end
You could also put the call to size before the if-else and then branch based on whether page == 1
This would save a call to isempty, but would reduce the readability of the code as the intent might be a little less obvious. Actually I was surprised that the size of the third dimension of an empty array is 1 and not zero, but apparently that is how they implemented the size function. I think it would be better practice to just check if MDA is empty using isempty

More Answers (0)

Community Treasure Hunt

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

Start Hunting!