Matrix addition from multiple files
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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 件のコメント
Gollapalli Prince
2019 年 4 月 12 日
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)
Gollapalli Prince
2019 年 4 月 15 日
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))
Jon
2019 年 4 月 15 日
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.
Gollapalli Prince
2019 年 4 月 22 日
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
Jon
2019 年 4 月 22 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
