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

조회 수: 1 (최근 30일)
Jason
Jason 2019년 10월 4일
댓글: Jason 2019년 10월 4일
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

채택된 답변

Jon
Jon 2019년 10월 4일
편집: Jon 2019년 10월 4일
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
  댓글 수: 1
Jason
Jason 2019년 10월 4일
Yes thats what messed me up, when you reset, the "page" still remained 1.
Thnaks for your help

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by