필터 지우기
필터 지우기

Save double vector in matrix in for loop

조회 수: 1 (최근 30일)
Bruce Rogers
Bruce Rogers 2021년 7월 2일
댓글: Bruce Rogers 2021년 7월 2일
Hello everyone,
I'm trying to safe the values of a variable into a matrix. The calculations happen in a for loop, but only the last iteration will be saved into the matrix. Is there an error in my code or is it just wrong how I'm trying to save the double vector into the matrix?
I'll put some part of the code below. Just tell me, if you need the whole code.
Thanks for your help!
for j = 1:5
%... a long code with calculations to get the phase shift of two sine
%curves
% time difference for positive slope crossing points
dt_pos_slope = t0_pos1 - t0_pos2;
% time difference for negative slope crossing points
dt_neg_slope = t0_neg1 - t0_neg2;
d_df_mat = zeros(10,5);
d_df = [dt_neg_slope dt_pos_slope]';
d_df = abs(d_df(:));
d_df_mat(:,j) = d_df; %Here I'm trying to save the values
%into the d_df_mat Matrix, but only the last iteration will be saved
end
  댓글 수: 1
Yazan
Yazan 2021년 7월 2일
In each iteration, you are redefining the matrix d_df_mat and setting its values to zeros. Therefore, only the last iteration will survive. Define your matrix d_df_mat outside the loop.

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

채택된 답변

Matt J
Matt J 2021년 7월 2일
편집: Matt J 2021년 7월 2일
The line,
d_df_mat = zeros(10,5);
should be prior to the loop, not inside it.
  댓글 수: 1
Bruce Rogers
Bruce Rogers 2021년 7월 2일
Oh yes, you're right. Thanks for your help, I didn't notice

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

추가 답변 (1개)

KSSV
KSSV 2021년 7월 2일
d_df_mat = zeros(10,5);
for j = 1:5
%... a long code with calculations to get the phase shift of two sine
%curves
% time difference for positive slope crossing points
dt_pos_slope = t0_pos1 - t0_pos2;
% time difference for negative slope crossing points
dt_neg_slope = t0_neg1 - t0_neg2;
d_df = [dt_neg_slope dt_pos_slope]';
d_df = abs(d_df(:));
d_df_mat(:,j) = d_df; %Here I'm trying to save the values
%into the d_df_mat Matrix, but only the last iteration will be saved
end

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by