Some problems using map/container with if-else statement in nested for loops

조회 수: 3 (최근 30일)
Hi everyone, this is my following code with the use of map and container with if-else statement in nested for loops. I don't understand why the error Unrecognized function or variable 'Intensitysol1' appears, while there is no problem for Intensitysol2. How should I fix it?
theta=0.65;
J=0.08;
for ii = 0:1:49;
omega_y(ii+1) = 0 + (0.15/100)*(ii);
end
for jj = 0:1:100;
omega_z(jj+1) = 0 -(0.15/100)*(jj);
end
valueset_omega_y_vector = 1:1:(length(omega_y));
valueset_omega_z_vector= 1:1:(length(omega_z));
M_omega_y = containers.Map(omega_y,valueset_omega_y_vector);
M_omega_z = containers.Map(omega_z, valueset_omega_z_vector);
for ii = 1:1:49
for jj = 0:1:100
u = 0 : 2*pi/1000 : 2*pi;
E_minus = 2*J*cos(theta).*cos(u) - (sqrt(omega_y(ii+1)^2 + omega_z(jj+1)^2 + 4*(J^2)*((sin(theta))^2).*((sin(u)).^2) -4*J*omega_z(jj+1)*sin(theta).*sin(u)));
TF = islocalmin(E_minus);
a=u(TF);
if size(a)==2
Intensitysol1(M_omega_y(omega_y(ii+1)),M_omega_z(omega_z(jj+1)))=a(1);
Intensitysol2(M_omega_y(omega_y(ii+1)),M_omega_z(omega_z(jj+1)))=a(2);
elseif size(a)==1
Intensitysol2(M_omega_y(omega_y(ii+1)),M_omega_z(omega_z(jj+1)))=a(1);
end
end
end
figure(1)
surf(omega_z, omega_y, Intensitysol1)
Unrecognized function or variable 'Intensitysol1'.
xlabel('\omega_z')
ylabel('\omega_y')
zlabel('k')
figure(2)
surf(omega_z, omega_y, Intensitysol2)
xlabel('\omega_z')
ylabel('\omega_y')
zlabel('k')

채택된 답변

Voss
Voss 2024년 6월 29일
편집: Voss 2024년 6월 29일
Intensitysol1 is not defined because the condition "if size(a)==2" is never true. size() returns a vector, and comparing a vector to a scalar in an if statement evaluates to true only if the condition is true for all elements of the vector, which is not the case here since the size of "a" will be 1-by-something (since "a" is a row vector). That is, size(a) is never a vector of all 2's.
You probably mean "if isequal(size(a),[1 2])", or, simpler, "if length(a)==2" or "if numel(a)==2".
Note that "if size(a)==1" works because "a" would be of size 1x1, so size(a)==[1 1], and that condition is considered true since all elements of the size are equal to 1. But here as well it is clearer to use length() or numel() instead of size().

추가 답변 (0개)

카테고리

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

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by