I'm having trouble saving the matrix?

조회 수: 5 (최근 30일)
studentmatlaber
studentmatlaber 2022년 4월 14일
댓글: Walter Roberson 2025년 2월 24일
Mxest1=(x_T_est1)';
Mxest1(isnan(Mxest1)) = [] %NANs
Myest1=(y_T_est1)';
Myest1(isnan(Myest1)) = [] %NANs
for n=1:length(Mxest1)
mae1=sum(sqrt((x_T-Mxest1(n)).^2+(y_T-Myest1(n)).^2));
end
mae1(1,1)=mae1/n
save('mae1.mat','mae1')
%for new value Mxest1 and Myest1
Mxest1=(x_T_est1)';
Mxest1(isnan(Mxest1)) = [] %NANs
Myest1=(y_T_est1)';
Myest1(isnan(Myest1)) = [] %NANs
for n=1:length(Mxest1)
mae1=sum(sqrt((x_T-Mxest1(n)).^2+(y_T-Myest1(n)).^2));
end
mae1(1,2)=mae1/n
save('mae1.mat','mae1')
Hello. I have 51 variegated Mxest1 and Myest1 values. I enter these values manually. As a result, the matrix I want should be mae1=1x51. After manually entering the first value, I save the first result at mae1(1,1). Then I want to save the second value at mae1(1,2). But it doesn't do it correctly. How can I do that? I would be glad if you help.
  댓글 수: 1
Benjamin Thompson
Benjamin Thompson 2022년 4월 14일
You have not defined x_T_est1 in your code listing.

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

답변 (1개)

Shreshth
Shreshth 2025년 2월 24일
Hi,
I understand you would like to store the output from each loop in mae1.
You can achieve this by using a temporary variable “temp” and storing the result inside the “for” loop in “temp” variable. After exiting loop, you can store the value of “temp” variable in “mae1”.
for n=1:length(Mxest1)
temp=sum(sqrt((x_T-Mxest1(n)).^2+(y_T-Myest1(n)).^2));
end
mae1(1,1)=temp/n;
Repeat the same for mea1(1,2).
Hope this helps!
  댓글 수: 1
Walter Roberson
Walter Roberson 2025년 2월 24일
The above code shares the problem of the original code. Each iteration of the for loop, the temp variable is being completely overwritten. You might as well only perform the final iteration of the loop, for n=length(Mxest1) because the previous iteration's output are going to be discarded.
You can
for n=1:length(Mxest1)
temp(n)=sum(sqrt((x_T-Mxest1(n)).^2+(y_T-Myest1(n)).^2));
end
but then you run into the problem:
mae1(1,1)=temp/n;
The right hand side is now a vector, but the assignment is to a scalar location. It is not at all clear what the desired output is.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by