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!