Effacer les filtres
Effacer les filtres

How can I combine every Nth row of a matrix into a new matrix using a loop?

17 vues (au cours des 30 derniers jours)
Jane
Jane le 5 Oct 2012
Commenté : Walter Roberson le 10 Mai 2020
I have a huge matrix A:
A=[X,Y]
and would like to create a new matrix B using every Nth row of A:
B=[A(1,:); A((1+N),:); A((1+2N),:); ...; A((1+(length(X)-N)),:)]
where length(X)>>1000.
How can I create this new matrix B using a loop?

Réponse acceptée

Thomas
Thomas le 5 Oct 2012
Modifié(e) : Thomas le 5 Oct 2012
Is this what you want? You do not need to use loops
% initial data
a=[1:10;11:20]'
% select every 3rd row
out = a(1:3:end,:)
  3 commentaires
Ransika Pathirana
Ransika Pathirana le 10 Mai 2020
If i need to go through a for loop how am i going to do it?
Thank you!
Walter Roberson
Walter Roberson le 10 Mai 2020
r__r_ = ceil(size(A,1)/N);
B = zeros(r__r_, size(A,2), 'like', A);
for r__r__ = 1 : r__r_
B(r__r__,:) = A(1 - N + r__r__*N, :);
end

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by