# How to pick only one non-zero value from each row of a matrix (iterative output)

1 view (last 30 days)
Adnan Barkat on 3 Mar 2022
Commented: Adnan Barkat on 17 Mar 2022
Hi everyone,
My question has two parts:
Part_1: Iterative calculation
I required to repeat my iterative calculation for diffrent initial guesses. For exmaple, in the below script where i use n0=1000 as initial guess. May someone help me to mopdify this script for different initail guesses (0, 10, 100, 1000) and save the out put in a single csv file.
My script is as follow for iterative calculation:
for i=1:length(A);
f = @(n)((n+1)*log(n+1)-n)/n^2 - A(i);
n0 = 10000; % Initial guess
n(i) = fzero(f,n0 );
disp(n );
end
nn=n';
csvwrite('N_mean_10000.csv',nn);
Part_2: the output matrix is consist of either non-zero values or NaN, but non-zero values are same in each row. So i wnat to pick only one non-zero values and save in another matrix. The output is liek this: KSSV on 3 Mar 2022
m = length(A) ;
IG = [0, 10, 100, 1000] ; % initial guess
n = length(IG) ;
nn = zeros(m,n) ;
for i = 1:n
n0 = IG(i) ;
for j=1:m
f = @(n)((n+1)*log(n+1)-n)/n^2 - A(j);
nn(i,j) = fzero(f,n0 );
disp(nn(i,j) );
end
end
csvwrite('N_mean_10000.csv',nn);
Adnan Barkat on 17 Mar 2022
I double check the A (a column matrix with all positive non-zero enteries)