Subtracting matrices by column and performing a summation.

조회 수: 1 (최근 30일)
Lucas
Lucas 2024년 9월 30일
편집: Voss 2024년 10월 1일
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!

채택된 답변

Voss
Voss 2024년 9월 30일
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
Lucas
Lucas 2024년 10월 1일
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
Lucas
Lucas 2024년 10월 1일
I am posting this as another question too, sorry about that, should've done that first so you can reply to it.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2024년 9월 30일
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
Lucas
Lucas 2024년 9월 30일
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!
Steven Lord
Steven Lord 2024년 9월 30일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by