MATLAB: Replacing the non zero values of matrix by another values based on some criterion.

matrixmatrix manipulationwireless communication

Hello,
I have a channel matrix similar to the given matrix.
A=[1 0 0 0; 0 0 12 0; 0 2 0 0; 8 0 0 0; 0 9 0 0; 0 0 0 8; 4 0 0 0; 0 0 0 3; 0 5 0 0; 0 0 6 0]
I have to find the non-zero element (channel gain) in each column and multiply them by power factor in ascending order of their channel gains. And update the matrix. The value of power factor is calculated as
2*i/(K*(K+1)). Where k is total number of non-zero elements in each column and the value of I varies as: i=1,2,,3….K. . For example, in column 1 there are three non-zero element (1, 8, 4). Then power allocation is as follows:
Ch gain: 8>4>1
Power factor: 2*1/(3*4), 2*2/(3*4), 2*3/(3*4) for 8, 4,1 respectively.
Problem: I am able to find first maximum of each column and able to update its value but not able to do it for other. Even in the final matrix only the first max value of column is updated. If anyone have idea please help me. The raw structure of code is given here.
A=[1 0 0 0; 0 0 12 0; 0 2 0 0; 8 0 0 0; 0 9 0 0; 0 0 0 8; 4 0 0 0; 0 0 0 3; 0 5 0 0; 0 0 6 0]
for j=1:1:4
Totl_users_to_each_BS(:,j)=sum(A(:,j)~=0)
end
T= Totl_users_to_each_BS;
%%%%% power allocation factor values
for j=1:1:4
for k=1:1:T(1,j)
%NAV= 2.*k.*A(i,j)./(T(1,j).*(T(1,j)+1))
NAV= 2.*k./(T(1,j).*(T(1,j)+1))
end
end
for j=1:1:4
[A1 I] =max (A(:,j))
%[A2 I2]= max (A(:,j))
A3=A(I).*5%%% we need to take NAV here
A(I)=A3
end

Best Answer

If I understood correctly,
A = [1 0 0 0; 0 0 12 0; 0 2 0 0; 8 0 0 0; 0 9 0 0; 0 0 0 8; 4 0 0 0; 0 0 0 3; 0 5 0 0; 0 0 6 0];
[sortedA, roworder] = sort(A, 1, 'descend');
K = sum(sortedA ~= 0, 1);
powerfactor = 2 * (1:size(A, 1))' ./ (K .* (K+1)); %your 2*i/(K*(K+1))
Afactor = zeros(size(A));
Afactor(sub2ind(size(A), roworder, repmat(1:size(A, 2), size(A, 1), 1))) = powerfactor; %reorder powerfactor according to original order of A
result = A .* Afactor