Add large amount of variables

조회 수: 2 (최근 30일)
Magnus Schneider
Magnus Schneider 2022년 5월 9일
댓글: Magnus Schneider 2022년 5월 9일
Hello, I have a set of variables which are all Matrixes and their name is usually "Hsig_day_time". I would like to add them all but it there are about 120 and possibly there will be more than 1000.
Is there a way to add them all without writting each variable name in a line?
  댓글 수: 7
Magnus Schneider
Magnus Schneider 2022년 5월 9일
@Rik These variables are created by an external software. I am not sure if it is possible to load them as a struct. At least I can't do it.
Stephen23
Stephen23 2022년 5월 9일
"I am not sure if it is possible to load them as a struct. At least I can't do it."

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

채택된 답변

dpb
dpb 2022년 5월 9일
OK, I'll just make another Answer so previous conversation doesn't distract
Brute force; may be something a little more clever possible, but...
S=load('IBERIA.mat','-regexp', '^Hsig'); % load the variables beginning with Hsig only
C=struct2cell(S); % turn the fields into cells in cell array
A=C{1}; % begin accumulator for sum
for i=2:numel(C) % and iterate over the size of C
A=A+C{i}; % sum each array
end
mnA=A/numel(C); % compute mean
Above doesn't deal with NaN -- I noticed there are at least some matrices with NaN in them; I didn't search for which or where. I did note that the first 10 locations in both directions are finite.
You'll know better what to do with those locations...
  댓글 수: 3
Image Analyst
Image Analyst 2022년 5월 9일
@Magnus Schneider can you please click the "Accept this answer" link to award @dpb "Reputation Points" and to let others know it's been solved. Thanks in advance. 🙂
Magnus Schneider
Magnus Schneider 2022년 5월 9일
yes of course.

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

추가 답변 (1개)

dpb
dpb 2022년 5월 9일
편집: dpb 2022년 5월 9일
Don't try to explain in words; show us precisely what the external software generates.
" They are all inside a .mat file..."
Then at a minimum show us what
whos -file TheFileName.mat
returns for one of these files.
Even better would be to attach one here..."help us help you"
The initial (following) was written before I saw the above comment so presumed each was a separate file.
If they are 2D arrays and the dimensions are not humongous for each, then you can simply read each into a 3D array where each file is a plane and use mean(A,3)
If the total won't fit in memory, then a mean is simply a sum which can be computed as a running summation with only one file at a time, then divide the sum by the total number of files.
Either is a basically trivial task to iterate over all files in a particular location.
  댓글 수: 1
Magnus Schneider
Magnus Schneider 2022년 5월 9일
I am sorry this is the first time I am posting here. The file is in the attachment. The final objective is to obtain a Matrix with the averages of each element of the Hsig matrixes. In the real examples there will be hundreds of Hsig matrixes...

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

카테고리

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