Adding all the rows of a matrix

1 次查看(过去 30 天)
Khadija Khan
Khadija Khan 2018-4-11
I have a matrix A=[1 0 3 0 0; 0 1 0 1 0]; I have B=sum(A); B=[1 1 3 1 0]; After performing some operation on this matrix B let my matrix B is B=[0.1 0.1 0.25 0.1 0], How can I get back A=[0.1 0 0.25 0 0; 0 0.1 0 0.1 0]. Note: This operation can only be performed on Matrix B. Can anybody help? Thanks in advance
  3 个评论
Rik
Rik 2018-4-11
Have you seen what you posted? It is unreadable. Select the code you have in your comment and click the {}Code button.
And if you have working code, what is your question?
Khadija Khan
Khadija Khan 2018-4-11
编辑:Khadija Khan 2018-4-11
I solved it Thank you so much Rik Wisselink
Q=20;
Kr=2;
Kt=3;
A=[10 0 0 ; 0 22 0];
B=sum(A);
if (sum(B(1,:)) > Q)
sorted_matrix = sort(B(1,:),'descend');
N_tilde = size(sorted_matrix,2);
sorted_matrix(end+1) = -Inf;
while(N_tilde > 0)
if ((sorted_matrix(N_tilde) > sorted_matrix(N_tilde+1)) && ...
(sorted_matrix(N_tilde) > (sum(sorted_matrix(1:N_tilde))-Q)/N_tilde))
mu = (sum(sorted_matrix(1:N_tilde))-Q)/N_tilde;
B(1,:) = max(B(1,:) - mu, 0);
break;
else
N_tilde = N_tilde - 1;
end
end
end
A_temp=zeros(Kr,Kt);
for row=1:Kr
for col=1:Kt
if A(row,col)>0
A_temp(row,col)=1;
else
A_temp(row,col)=A(row,col);
end
end
end
A_final=zeros(Kr,Kt);
for r=1:Kr
A_final(r,:)=A_temp(r,:).*B;
end

请先登录,再进行评论。

回答(1 个)

Khadija Khan
Khadija Khan 2018-4-11
Rik Wisselink when I asked the Question, I was struggling with the code. Now it's working fine. Thanks

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by