Adding all the rows of a matrix

조회 수: 2 (최근 30일)
Khadija Khan
Khadija Khan 2018년 4월 11일
답변: 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에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by