MATLAB: How to use fprintf to print indexes of a matrix

floating pointfprintfmatrixparsing

I'm given a 50×10 matrix "A" of complex numbers and asked to write a program to search it for elements which meet certain criteria. The criteria requires a complex number's complex modulus to fall within +/- 10% of a target value of 5. If the criteria is met, this is a "hit". Each time a hit occurs, formatted data for that element must be stored in a new matrix B, and the information for the hit must be printed to the screen before the program's next iteration. The screen output for each hit should look like this:
Screen Shot 2018-12-15 at 8.24.20 PM.png
where the data printed represent 1) the element's location in A, 2) the complex number (real and imag components), 3) the complex modulus of the complex nunmber, and 4) the phase angle of the complex number.
Here's the code so far:
clc;
close all;
clear all;
% load external data
load matrix_data.mat
% creating variables
i = 1;
j = 1;
k = 1;
pct_diff = 0.1;
targetDiff = .5;
B(1,1) = 0;
hits = 0;
while (i<=50) % iterates through each row of
while (j<=10) % and column of A
modulusMinus5 = abs(abs(A(i,j))-5) % finds the absolute difference between target value and complex modulus
if (modulusMinus5<=targetDiff) % if the criteria is met
B(k,1) = real(A(i,j)); % store the following data in
B(k,2) = imag(A(i,j)); % a new matrix "B", which will
B(k,3) = abs(A(i,j)); % be used later for printing
B(k,4) = angle(A(i,j)); % data to the screen
k = k+1; % increment counter k to create a new row in matrix B
% print formatted data each time there's a hit
fprintf('A(%f,%f) = %f + %f j , r = %f , phi = %f\n', i,j,B(k,1), B(k,2), B(k,3), B(k,4));
end
j= j + 1; % increment j to continue searching the columns in row i
end
i = i + 1; % increment i to search the next column in matrix A
end
i only runs once and doesn't iterate (i=1). j increments 4 times (j = 5 before I get the error). k increments only once (k=2). The error is below:
Screen Shot 2018-12-15 at 8.01.34 PM.png
It looks like the error refers to i, which I want to print to show which row of A the hit was located in.
I've had no success looking for a solution on google. Any help would be greatly appreciated!
Marshall

Best Answer

Found my main problem! I changed my while loops to for loops and the program now checks each element in A. I now have 37 hits. Thanks for all the help, everyone! You set me on the right track!