Calculate table column with a loop
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
René Dienemann
am 6 Nov. 2019
Bearbeitet: Guillaume
am 6 Nov. 2019
Hi Matlabfriends,
I have a large table with data (120 column and 200 rows). I have to subtract column 2 (all rows) from column 1 (all rows),
column 3 (all rows) from column 2 (all rows), and so on, until column 120 (all rows). Can I solve this with a loop, how?
Thanks!
Here is my try (note: I'm an absolutley beginner):
for i=1:120
variable1(i,1:120)=table(i,1:2:119) - table(i,2:2:120);
end
0 Kommentare
Akzeptierte Antwort
Guillaume
am 6 Nov. 2019
Bearbeitet: Guillaume
am 6 Nov. 2019
As usual with matlab, it's simpler without a loop
result = diff(table2array(yourtable), [], 2); %difference between consecutive columns
Note that you may be better off using a matrix rather than a table for storing your original data.
edit: the above assumes that you're actually storing your data in a matlab table. But judging by your syntax you may actually be storing your data in matrix, in which case:
result = diff(yourmatrix, [], 2);
Whether a matrix or table, don't use table as a variable name, since it will prevent you from constructing tables.
0 Kommentare
Weitere Antworten (1)
Takumi
am 6 Nov. 2019
table = rand(200,120); %sample data
[row,col] = size(table);
for i = 1:col-1
variable1(:,i) = table(:,i+1) - table(:,i);
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!