Subtracting matrices by column and performing a summation.
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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!
0 commentaires
Réponse acceptée
Voss
le 30 Sep 2024
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]
% sum the absolute difference between adjacent rows in all columns
S = sum(abs(M(2:end,:)-M(1:end-1,:)),1)
7 commentaires
Plus de réponses (1)
Steven Lord
le 30 Sep 2024
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 commentaires
Steven Lord
le 30 Sep 2024
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.
Voir également
Catégories
En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!