Loop for creating Submatrix

조회 수: 3 (최근 30일)
Claas-Christoph Seidel
Claas-Christoph Seidel 2020년 9월 16일
댓글: Claas-Christoph Seidel 2020년 9월 17일
Hey, I am still pretty new to MatLab and have Problems with creating a Loop that stores specific Data from one Matrix in a new one. To be precise, i need every fourth row of a Matrix ´forQ´ to create a ´nSim x 20´ Matrix where the first row refers to the fourth row of Matrix `forQ´, the second to the 9th of ´forQ´, the third to the 13th of `forQ´ and so on. However, when using the loop below, the rows of my resulting Matrix ´x´ consist only of the values in column 80 of Matrix ´forQ´.To be more precise, each value of e.g. row 1 in ´x´ is equal to the value of row 4, column 80 of ´forQ´. But this is not what i intended. Basically i want to copy ´forQ´s 1000 values in row 4 into row 1 of matrix x. I hope you understood the problem.
nSim=1000
x=zeros(nSim,20);
for c=1:20
for d=4:4:80
x(:,c)=forQ(:,d);
end
end
Thanks in andvance !!

채택된 답변

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020년 9월 16일
It should not be two nested loops. You can use one loop that increases a counter.
You can use this code that doesn't need a loop:
x = forQ(4;4:end, :);
  댓글 수: 1
Claas-Christoph Seidel
Claas-Christoph Seidel 2020년 9월 17일
Thank you, you are absolutely right !

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by