Matrix addition from multiple files
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
I have 240 matrices stored in 240 files with the name DOS1.dat DOS2.dat ......DOS240.dat.
The matrix size is 300x19. How to do a matrix addition.
채택된 답변
Jon
2019년 4월 11일
You haven't provided enough detail to answer your question too specifically but in general the steps would be to read the individual data files into MATLAB and assign the data in each file to a matlab array. You could then perform the desired matrix addition on the elements of this matlab array. So for example:
% assign your dimensions
numRows = 300 % number of rows in your individual matrices
numCols = 19 % number of columns in your individual matrices
numMats = 240 % number of individual matrices
% preallocate array to hold the data
A = zeros(numRows,numCols,numMats);
% loop to read individual matrices from files
for k = 1:numMats
% build the name of the data file as a character vector'
filename = ['DOS',num2str(k)];
A(:,:,k) = readmatrix(filename);
end
% now for example add the first and third matrix
B = A(:,:,1) + A(:,:,3)
You may have to modify the above code to fit the specifics of your situation, but this should give you an idea of how it could be done.
댓글 수: 7
Hi Jonathan,
Thank you for your great help, but after running the script this is the error

Let me explain my question in detail so that you can help me better.
I have DOSx.dat files from DOS1.dat to DOS240.dat
Below is the screenshot (Shown partially )

DOS1.dat to DOS240.dat have same number of rows and columns.
I'm attaching DOS1.dat for overall idea. I cannot upload .dat file, hence I've changed the extension to .txt. Thank you. Please help me in matrix addition. DOS1.dat is the first matrix, DOS2.dat is second matrix and so on...
Hi Gollapalli,
The error you are getting is due to the fact that the MATLAB readmatrix function was just recently introduced, (I think in 2019A) so if you are running an older version of MATLAB you will not have this function available.
So either you can update your MATLAB, or use the older function csvread. In the latter case please use the following approach, and in particular note that on line 13 the filename now explicitly assigns the file extension .dat, and on line 15 the offending call to readmatrix is replaced by a call to csvread :
% assign your dimensions
numRows = 2; % number of rows in your individual matrices
numCols = 3; % number of columns in your individual matrices
numMats = 3; % number of individual matrices
% preallocate array to hold the data
A = zeros(numRows,numCols,numMats);
% loop to read individual matrices from files
for k = 1:numMats
% build the name of the data file as a character vector'
filename = ['DOS',num2str(k),'.dat'];
A(:,:,k) = csvread(filename);
end
% now for example add the first and third matrix
B = A(:,:,1) + A(:,:,3)
I've used online version of matlab 2019a: This is the error

This is the workspace

Matlab 2018a: offline version with your modified code (latest)

My matrix size is 301x19
Please help me also in looping through the sum:
matrix1+matrix2+matrix3+......matrix240
B=A(:,:,1)+A(:,:,2)+.......+A(:,:,240))
Hi Gollipalli,
I think that in both cases there seems to be a problem recognizing the delimiters in your file, which I now see are spaces, not commas. As a result, the full 301 x 19 matrix seems to be read incorrectly and thus does not have the correct size. I think there may be some kind of hidden characters in that file, maybe line ending characters that confuse it. I couldn't get the new readmatrix function to work but I could get dlmread (delimited read to work).
I recommend replacing the line
A(:,:,k) = csvread(filename)
with
A(:,:,k) = dlmread(filename,'')
I couldn't actually test this on your file, because for some reason when I try to download your attached file it just opens as webpage in my browser. I tried copying and pasting from there into a text file and the above line then seemed to work OK.
Regarding adding up the matrices once you have them in the big three dimensional array, A, this can be done with just the one line
Atotal = sum(A,3)
which adds across the third dimension.
Jonathan,
Finally the one below worked for me. Thanks for your time and effort
File=dir('path/*.dat');
for i=1:size(File,1)
A(:,:,i)=load(File(i,1).name);
end
sum = 0
for i=1:size(File,1)
sum = sum + A (:,:,i);
end
Hi Gollapalli,
I'm glad you finally got it working. I have only used the "load" command for loading .mat files, but I see now now from the documentation that it can also be used for loading "ascii" text files, which I guess is what you must have had. That's good to know.
Regarding your final loop to add the elements, in general it is good to take advantage of MATLAB's ability to efficiently handle such operations without loops. So you could replace the loop with
B = sum(A,3) % sum across the third dimension of A
Avoiding loops should make the code run faster, and also makes the code simpler to read.
Also, although MATLAB doesn't complain when you use a variable name that is the same as a MATLAB function, it can make things a little confusing. So in your case, your local variable named "sum" is actually taking the place of the MATLAB sum command. If later in your code you tried to use the "sum" command it would not work, as it would think that you were referring to your variable named "sum".
Jon
2019년 4월 22일
I noticed that you unaccepted the answer to this. I'm curious, which one did you find that was better? If there is a better answer, it would be good to make a comment pointing others toward it, in case they are following this thread, and it just seems to be left unanswered.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
