MATLAB Answers

Jason
0

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

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

  0 件のコメント

サインイン to comment.

1 件の回答

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
2019 年 10 月 4 日
Yes thats what messed me up, when you reset, the "page" still remained 1.
Thnaks for your help

サインイン to comment.



Translated by