0
votes

Why am I getting this error? The matrices are absolutely of the same size:

L=rand(4,1000);
 for i=1:1000;
    A(i)=logzn(0.1,0.4,L(4,i));
    B(i)=logzn(0.3,0.7,L(4,i));
    C(i)=logzn(0.5,1.0,L(4,i));
end
F=(~A&B | ~B&C);

Here's the logzn function:

function [ y ] = logzn( aMin,aMax,x )
if ((aMin<=x)&&(aMax>=x)) 
    y=1;
else
    y=0;
end

Here's the error I get: Error using & Matrix dimensions must agree.

1
Are you certain that the error is coming from these lines of code?Suever

1 Answers

0
votes

The error means exactly what it says, that A, B, and C are not the same size. You can check it with:

isequal(size(A), size(B)) && isequal(size(A), size(C))

That being said, the code as you've posted it doesn't have any issues; however if the error is in fact coming from these lines of code, you must have initialized A, B, and C to be different sizes somewhere else in the script (or you potentially used the same variable names previously and didn't clear them). You will want to pre-allocate them before the loop to ensure they are the same size.

L = rand(4, 1000);
[A, B, C] = deal(zeros(size(L,2), 1));

for k = 1:size(L,2);
    A(k)=logzn(0.1,0.4,L(4,k));
    B(k)=logzn(0.3,0.7,L(4,k));
    C(k)=logzn(0.5,1.0,L(4,k));
end

F=(~A&B | ~B&C);