Subtracting matrices by column and performing a summation.

Hello all. I am new to MatLab but I am hoping to use it for a component of my research. I am going to create matrices and hopfully perform calculations with them in MatLab.
I am going to have matrices that are 3 x X number of points. Usually they will be around 3 x 1500. I am trying to create a code that will subtract the second element value from the first element value for positions i j and k. It would perform this calculation all the way through 1500 lines and then perform a summation of each column at the end. Here is an example.
[0 0 10]
[2 5 8]
[-5 10 6]
Those would be the first 3 of the 1500 lines. The code would subtract 0 from 2 (take the absolute value of that), then add that to -5 minus 2 (take the absolute value of that) and continue that down. It would also subtract 0 from 5 (take the absolute value of that), then add that to 10 minus 5 (take the absolute value of that) and continue that down. Then it would subtract 10 from 8 (take the absolute value of that), then add that to 6 minus 8 (take the absolute value of that) and continue that down.
At then end I would have three values: An absolute value summation of the change in the first column of elements, an absolute value summation of the change in the second column of elements, and an absolute value summation of the change in the third column of elements.
Any help is much appreciated! I am learning MATLAB and will try this out on my own in the meantime. Thank you!

 Accepted Answer

3xX indicates 3 rows and X columns, but your subsequent explanation describes a matrix with X rows and 3 columns. I'm going to assume it's actually 3 columns.
% example matrix
M = [0 0 10; 2 5 8; -5 10 6]
M = 3×3
0 0 10 2 5 8 -5 10 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% sum the absolute difference between adjacent rows in all columns
S = sum(abs(M(2:end,:)-M(1:end-1,:)),1)
S = 1×3
9 10 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

7 Comments

Or, using the diff function:
% example matrix
M = [0 0 10; 2 5 8; -5 10 6]
M = 3×3
0 0 10 2 5 8 -5 10 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% sum the absolute difference between adjacent rows in all columns
S = sum(abs(diff(M,1,1)),1)
S = 1×3
9 10 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Awesome! Thank you so much @Voss that is super helpful! I will implement that into my code.
I have a follow up question relating to this. I want to perform this same calculation on 500 .csv files on my machine. Is there a way to call in all those .csv files and perform this same calculation on all of them. Maybe even export the end result of each calculation into a csv file. Or is that more of a python task? Thanks again, it means alot!
You're welcome!
"I want to perform this same calculation on 500 .csv files on my machine... is that more of a python task?"
You can do that in MATLAB or Python, whichever you prefer. For a discussion about processing a sequence of files in MATLAB, see this thread:
Essentially, you can use dir to get the relevant file names and locations, then for loop over the set of files, reading each file (e.g., using readmatrix or readtable), processing each file's data, and storing the results. After the loop write the results to another file (e.g., using writematrix or writetable).
Example:
% appropriate dir() call that returns info
% about the files you want to process:
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, 3 columns):
results = zeros(N_files,3);
% read and process each file:
for ii = 1:N_files
% read the file:
M = readmatrix(fullfile(fn(ii).folder,fn(ii).name));
% process the file's data:
S = sum(abs(diff(M,1,1)),1);
% store the result:
results(ii,:) = S;
end
% write the results file (can be located anywhere):
writematrix(results,'results.csv')
Wow! That is just what I needed. Thank you again @Voss. I did not know that MATLAB could do that. This is huge. That will save me hours of time on my research.
You're welcome! MATLAB is a wonderful tool.
I agree @Voss! I am playing around with my data some more today and I have another question. I looked around on the help area for this but I could not get it to work. I would like that same code in the above comment that you posted to read in part of the .csv file. For example, I would like it to calculate the sum(abs(diff(M,1,1)),1); for data starting at columns B10, C10, and D10 and then running ot the end of those columns. Can matlab point to certain data within a .csv?
Thanks! I will keep playing with it too.
Lucas
I am posting this as another question too, sorry about that, should've done that first so you can reply to it.

Sign in to comment.

More Answers (1)

Some functions you'll find useful are the diff, abs, and sum functions. The latter two are pretty easy to find in the doc by searching for terms that IMO are "obvious" (and that you used in your description like absolute value and summation.) The first may be a little more difficult to find, but I checked and searching for difference found it. [I was actually kind of expecting that to find a different function first.]

2 Comments

Thank you @Steven Lord. I appreciate the help! I have a follow up question relating to this. I want to perform this same calculation on 500 .csv files on my machine. Is there a way to call in all those .csv files and perform this same calculation on all of them. Maybe even export the end result of each calculation into a csv file. Or is that more of a python task? Thanks again, it means alot!
What part of this question are you experiencing the most difficulty with, importing the data or iterating over the list of files? If it's importing the data files, see this section of the documentation. If it's iterating over the list of files, take a look at the for keyword.

Sign in to comment.

Categories

Products

Release

R2024a

Asked:

on 30 Sep 2024

Edited:

on 1 Oct 2024

Community Treasure Hunt

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

Start Hunting!