How to perform matrix math
3 ビュー (過去 30 日間)
古いコメントを表示
Hello all, I currently have a code to look at delta distances in a .csv file and ouput a new csv files with these a summation of these distances for each of the three columns. This code looks at three columns in a .csv files, subtracts the second row from the first row and continues this calculation down all the rows. I have had the chance to look at some of this distance data and it looks good but I need to tweak my calculation. Where I have S = sum(abs(diff(M(:,2:4),1,1)),1).
I would like to continue this method of subtracting the second line from the first and moving down. But I need an intermediate step (or 2) where the values are subtracted values are squared. The that row of values would be added up and the sqrt of that would be taken.That end value would be added to the end values of that operation performed down the rows.
I have attached an image that explains this clearer (I hope). Its a way of doing the distance formula. I just need to loop it for 500 x y and z coordinates. I will also attach an example file of the data in .csv form. Thank you, any help is much appreciated!
clc
% appropriate dir() call that returns info
% about the files you want to process:
fn = dir('C:\Users\lucasarsenith\Desktop\Data/*.csv'); % this call returns info about .csv files in the current directory;
% you may need to modify it to work for your file locations
% (see dir documentation)
% number of files:
N_files = numel(fn);
% pre-allocate results matrix (one row per file, 3 columns):
results = zeros(N_files,3);
% read and process each file:
for ii = 1:N_files
% read the file starting from line 10:
M = readmatrix(fullfile(fn(ii).folder,fn(ii).name));
% process columns 2-4 of the file's data:
S = sum(abs(diff(M(:,2:4),1,1)),1);
% store the result:
results(ii,:) = S;
end
% write the results file (can be located anywhere):
writematrix(results,'C:\Users\lucasarsenith\Desktop\Plot.csv')
0 件のコメント
採用された回答
Voss
2024 年 10 月 2 日
fn = dir('*.csv'); % this call returns info about .csv files in the current directory;
% you may need to modify it to work for your file locations
% (see dir documentation)
% number of files:
N_files = numel(fn);
% pre-allocate results matrix (one row per file, 1 column):
results = zeros(N_files,1);
% read and process each file:
for ii = 1:N_files
% read the file:
M = readmatrix(fullfile(fn(ii).folder,fn(ii).name));
% process columns 2-4 of the file's data:
S = sum(sqrt(sum(diff(M(:,2:4),1,1).^2,2)),1);
% store the result:
results(ii,:) = S;
end
results
その他の回答 (1 件)
dpb
2024 年 10 月 2 日
編集済み: dpb
2024 年 10 月 2 日
Parallel to @Voss again with alternative MATLAB syntax and again reading the specific desired columns. What happened to the header lines??? although note code works setting the input number to zero to reproduce same result...
fn=dir('*.csv');
nHdr=0; % header lines to skip
C=["B";"D"]; % start, ending column (adjacent) --> "B:D" here...change to suit
range=join(C,":"); % build a range expression
N_files=numel(fn);
results=zeros(size(fn));
for ii=1:N_files
M = readmatrix(fullfile(fn(ii).folder,fn(ii).name), ...
'NumHeaderLines',nHdr,'Range',range);
results(ii,:)=sum(vecnorm(diff(M,1,1),2,2),1); % add Euclidean norm by row
end
disp(results)
3 件のコメント
dpb
2024 年 10 月 2 日
@Lucas, while I understand your sticking with what you already had from @Voss in Accepting an answer, you might give a "helping hand" vote to alternate solutions... :)
I haven't tried timing the difference between the ".*" direct calculation vis a vis using vecnorm; it might be interesting to see if you have a really large file to try both ways and see if there is any measurable difference or if the extra flexibility built into vecnorm might even make it slower than the direct calculation.
参考
カテゴリ
Help Center および File Exchange で File Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!