Extracting every row from a matrix using a for loop
Mostrar comentarios más antiguos
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!
Respuestas (1)
Rik
el 15 de Oct. de 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 comentarios
Edward Keavney
el 15 de Oct. de 2021
Yes, that is what this code does. You can see for yourself. I would recommend trying this with a small example:
data=(1:5).'+rand(5,2);% row x will have values x.####
for row=1:size(data,1)
rowdata=data(row,:);
fprintf('row %d contains:\n',row)
disp(rowdata)
end
See?
If you want to use the entire matrix at once, there is no need to index it at all. If what I showed isn't what you want, please given an example of what it is you want.
Edward Keavney
el 15 de Oct. de 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
But it is better to use mean on the entire array:
mean(data,2) % specify the dimension to make it operate on the rows
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!