How to perform matrix math

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')

 Accepted Answer

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
results = 291.6091

2 Comments

Thank you @Voss. That works great. It also matches my calculations in Excel.
Thanks again!
You're welcome!

Sign in to comment.

More Answers (1)

dpb
dpb on 2 Oct 2024
Edited: dpb on 2 Oct 2024
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)
291.6091

3 Comments

Epic, thank you @dpb. That works for me. I redid the calculation in Excel and that 291.6091 value is spot on. I wrote a script to get rid of the headers ahead of time but I may need that later on so thanks for including that!
Lucas
@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.
@dpb sounds good, I did not know you could give a helping hand. I will definetely play around with it to see if there are some differences. I appreicate your time and comments!
Lucas

Sign in to comment.

Categories

Products

Release

R2024a

Asked:

on 2 Oct 2024

Commented:

on 3 Oct 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!