Operands to the and && operators must be convertible to logical scalar values

조회 수: 1 (최근 30일)
My script takes zeros(3,3,countX_dn,countY_dn,countZ_dn) and uses central differencing to calculate the values inside ignoring the outermost layer. I need to fill in the outer layer so that each data point inside has the same value as its nearest neighbors. For the corner values, I need to make the nearest 7 points in the outer layer equal to the data point P. I repeat this by moving in x,y, and z with either the nearest 3 points or the nearest single point. I have included the first part of my script below (since the rest follows the same process), when I get to this line I get the following error "Operands to the and && operators must be convertible to logical scalar values." I thought I could use the "and logical" and the "or logical" in an if statement simply by listing what conditions I wish the if statement to verify, and run what is inside based on if that statement is true. What should I do to get rid of this error? I am using R2012a when I am writing my script.
I would be happy to provide the rest of my code if that helps but it is very long and this is the only error I am getting so far.
for k=2:(countZ_dn-1)
for j=2:(countY_dn-1)
for i=2:(countX_dn-1)
P = vel_grad(:,:,i,j,k);
% 6 Faces
F1 = vel_grad(:,:,i-1,j,k); F2 = vel_grad(:,:,i+1,j,k);
F3 = vel_grad(:,:,i,j-1,k); F4 = vel_grad(:,:,i,j+1,k);
F5 = vel_grad(:,:,i,j,k-1); F6 = vel_grad(:,:,i,j,k+1);
if F1==0 && F3==0 && F5==0
vel_grad(:,:,i-1,j,k) = P; % Face 1
vel_grad(:,:,i,j-1,k) = P; % Face 3
vel_grad(:,:,i,j,k-1) = P; % Face 5
vel_grad(:,:,i-1,j-1,k) = P; % Edge 1
vel_grad(:,:,i,j-1,k-1) = P; % Edge 2
vel_grad(:,:,i-1,j,k-1) = P; % Edge 5
vel_grad(:,:,i-1,j-1,k-1) = P; % Corner 1
end
% this repeats for 56 iterations so I only put the first in
% here since this is where the error appears
end
end
end

채택된 답변

Star Strider
Star Strider 2016년 8월 6일
In array (vector or matrix) comparisons, the short-circuit operators won’t work. You ahve to use the single operators ‘&’ instead for both, depending on what you want to do. Also, You might find the any and all functions helpful here, since equating arrays without those operators confuses MATLAB (and would confuse us as well).
  댓글 수: 2
Nathaniel Werner
Nathaniel Werner 2016년 8월 6일
I tried using the single & but Matlab tells me I should change it to &&.
Star Strider
Star Strider 2016년 8월 6일
That is probably just a warning, not an error.
This works:
F1 = rand(3);
F3 = zeros(3);
F5 = zeros(3);
if all(F1==0) & all(F3==0) & all(F5==0)
test1 = true
else
test1 = false
end
F1 = zeros(3); % Re-Define ‘F1’
if all(F1==0) & all(F3==0) & all(F5==0)
test2 = true
else
test2 = false
end
test1 =
0
test2 =
1

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

추가 답변 (0개)

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by