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!
How can I use fprintf to print indexes of a matrix?
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Marshall Prince
am 16 Dez. 2018
Beantwortet: Marshall Prince
am 16 Dez. 2018
I'm given a 50x10 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:

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:

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
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (2)
madhan ravi
am 16 Dez. 2018
Bearbeitet: madhan ravi
am 16 Dez. 2018
Note: If only your condition is met B gets filled with certain numbers else they are only zeros as per the preallocation , converted your while loops to for loops for conveniency.
clc;
close all;
clear all;
% load external data
load matrix_data.mat
% creating variables
k = 0;
pct_diff = 0.1;
targetDiff = .5;
B=zeros(50,4); % preallocate
counts=0;
hits = 0;
for i=1:50 % iterates through each row of
for j=1:10 % and column of A
modulusMinus5 = abs(abs(A(i,j))-5); % finds the absolute difference between target value and complex modulus
if abs(modulusMinus5-targetDiff)<=1e-4 % if the criteria is met , you're working with float numbers so add tolerance
hits = hits + 1;
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
% 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));
else
counts = counts + 1; % to know how many times the conditions were not satisfied
end
end
k = k+1; % increment counter k to create a new row in matrix B %% should be placed between the two loops
end
After running the code just type k in command window , if you get 50 for k then we are right meaning 50 rows had been touched. Matrix a has 500 elements so counts + hits should result to 500 meaning all the elements were accessed.
1 Kommentar
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

