2
votes

I want to create a nested for loop that pulls 1x2 double elements from a 1x4 cell array V and adds them together. 1x2 double elements contain numbers (ex. [0.234 0.325]). I want to add those four 1x2 double elements together and store the value in an incrementing counter VSum. However, MATLAB is returning [NaN NaN]. How do I fix this?

Additional info: A is a 50x50 cell array

VSum = [0 0];

for i = 1:1:size(G,2)
    for j = 1:1:size(G,1)
        for p = 1:1:4
            V{p} = (A{i,j}{p}*Q)/(2*pi*norm(Z{i,j}{p}));
            VSum = V{p}+VSum;
        end 
    end
end
1
what is Z? I'm guessing there is a 0 somewhere in Z - TinyTheBrontosaurus
or at least norm(Z{i,j}{p}) is 0 somewhere - TinyTheBrontosaurus
@TinyTheBrontosaurus Apologies, I should have mentioned that. Z is a 50x50 cell array just like A. Z contains many 1x4 cells. I am not sure if a 0 is present in any of those cells. - JaviSpartan

1 Answers

0
votes

If just a single value in the three-dimensional cell-array Z is 0, then this will return [NaN NaN]. Here's more details on where NaN vs Inf come from. Just a 0 in the denominator will create Inf (or -Inf) but adding Inf + -Inf would probably create a NaN