How to perform matrix math

3 次查看(过去 30 天)
Lucas
Lucas 2024-10-2
评论: Lucas 2024-10-3
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')

采纳的回答

Voss
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
results = 291.6091
  2 个评论
Lucas
Lucas 2024-10-2
Thank you @Voss. That works great. It also matches my calculations in Excel.
Thanks again!

请先登录,再进行评论。

更多回答(1 个)

dpb
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)
291.6091
  3 个评论
dpb
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.
Lucas
Lucas 2024-10-3
@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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by