Extracting every row from a matrix using a for loop

47 Ansichten (letzte 30 Tage)
Edward Keavney
Edward Keavney am 15 Okt. 2021
Kommentiert: Rik am 15 Okt. 2021
I'm working with some velocity data and have a matrix 23,999x31.
I'm new to using Matlab, I'm able of extracting one row but would now like to be able to create a for loop that will allow me to extract each of the 23,999 rows in the matrix.
Any help for a Matlab novice would be greatly appreciated!

Antworten (1)

Rik
Rik am 15 Okt. 2021
This might not be optimal, depending on what you want to do, but the size function is here to help:
data=rand(23999,31)
for row=1:size(data,1)
rowdata=data(row,:);
%do something with that row here
end
  4 Kommentare
Edward Keavney
Edward Keavney am 15 Okt. 2021
Understood, thanks. I've been approaching the problem in the wrong way. I want to be able to determine the mean from each of the 23,999 rows. Can you help with this?
Rik
Rik am 15 Okt. 2021
You can do that with the code I already posted:
data=(1:5).'+rand(5,2);% row x will have values x.####
for row=1:size(data,1)
rowdata=data(row,:);
mean(rowdata) %you can store this in a vector
end
ans = 1.1014
ans = 2.4187
ans = 3.5720
ans = 4.6323
ans = 5.6558
But it is better to use mean on the entire array:
mean(data,2) % specify the dimension to make it operate on the rows
ans = 5×1
1.1014 2.4187 3.5720 4.6323 5.6558

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by