Replacing the non zero values of matrix by another values based on some criterion.
2 views (last 30 days)
Show older comments
Shweta Rajoria
on 2 May 2019
Commented: Shweta Rajoria
on 3 May 2019
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
3 Comments
Adam
on 2 May 2019
You can return the indices as the second argument from the sort function and put them back in their original order afterwards.
Accepted Answer
Guillaume
on 2 May 2019
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
3 Comments
Guillaume
on 3 May 2019
It doesn't matter if powerfactor is calculated for i > K since these values will be multiplied by 0. You get the correct values anyway for i <= K.
Since you didn't specify a version of matlab (up there on the right of the page, there's a field for you to enter your version), I assume you were on a more or less up to date version. If you're on a very old version (< R2016b), then yes the powerfactor line will error, in that case:
powerfactor = 2 * bsxfun(@rdivide, (1:size(A, 1))', K .* (K+1)); %for versions <R2016b
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!