MATLAB Answers

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

3 views (last 30 days)
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
% this repeats for 56 iterations so I only put the first in
% here since this is where the error appears

Accepted Answer

Star Strider
Star Strider on 6 Aug 2016
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).
Star Strider
Star Strider on 6 Aug 2016
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
test1 = false
F1 = zeros(3); % Re-Define ‘F1’
if all(F1==0) & all(F3==0) & all(F5==0)
test2 = true
test2 = false
test1 =
test2 =

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!