How do I create a matrix from from the answers of each iteration of a for loop?

조회 수: 1 (최근 30일)
This is my for loop code,
N = 4
for yn = cos(pi*(0:N)/N)
yp = cos(pi*(0:N)/N);
bottom = 1./(yn - yp)
end
This is the output,
bottom =
Inf 3.4142 1.0000 0.5858 0.5000
bottom =
-3.4142 Inf 1.4142 0.7071 0.5858
bottom =
-1.0000 -1.4142 Inf 1.4142 1.0000
bottom =
-0.5858 -0.7071 -1.4142 Inf 3.4142
bottom =
-0.5000 -0.5858 -1.0000 -3.4142 Inf
How do I create a
[Inf 3.4142 1.0000 0.5858 0.5000
-3.4142 Inf 1.4142 0.7071 0.5858
-1.0000 -1.4142 Inf 1.4142 1.0000
-0.5858 -0.7071 -1.4142 Inf 3.4142
-0.5000 -0.5858 -1.0000 -3.4142 Inf] matrix from my output, because the last bottom iteration is the final answer?

채택된 답변

madhan ravi
madhan ravi 2020년 12월 17일
N = 4;
yn = cos(pi * ( 0 : N ) / N);
yp = cos(pi * ( 0 : N ) / N);
yp = reshape(yp, 1, 1, []);
bottom = squeeze(1 ./ (yn - yp))
bottom = 5×5
Inf 3.4142 1.0000 0.5858 0.5000 -3.4142 Inf 1.4142 0.7071 0.5858 -1.0000 -1.4142 Inf 1.4142 1.0000 -0.5858 -0.7071 -1.4142 Inf 3.4142 -0.5000 -0.5858 -1.0000 -3.4142 Inf
  댓글 수: 1
Syed Azib Anaqy Al-Sahab bin Wan Madhi
편집: Syed Azib Anaqy Al-Sahab bin Wan Madhi 2020년 12월 18일
Thank you! Can you explain how yn - yp does not equal to zero here? And also how reshape and squeeze works in this situation?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by