how to sum all row values in a matrix except the ith row?

조회 수: 8 (최근 30일)
chan
chan 2021년 9월 29일
댓글: DGM 2021년 9월 30일
i have a matrix X=rand([6,3,6]);
I want all the row value sum=1 except the ith row? this ith can be 4/5 according to user's choice? all the matrices row sum will be 1 except the ith row in every matrices? Here i dnt want to exclude the ith row. the elements of the ith row will be there as it is. could someone help me in solving this problem

채택된 답변

DGM
DGM 2021년 9월 29일
편집: DGM 2021년 9월 29일
There are probably other ways, but here's a way (if I understand the question correctly).
A = rand(6,3,6);
dnn = 5; % do not normalize this row
Adn = A(dnn,:,:); % save this row
A = A./sum(A,2); % normalize
A(dnn,:,:) = Adn; % replace the row
sum(A,2) % show that the row sums are 1 except where specified
ans =
ans(:,:,1) = 1.0000 1.0000 1.0000 1.0000 1.3225 1.0000 ans(:,:,2) = 1.0000 1.0000 1.0000 1.0000 1.3834 1.0000 ans(:,:,3) = 1.0000 1.0000 1.0000 1.0000 1.9078 1.0000 ans(:,:,4) = 1.0000 1.0000 1.0000 1.0000 1.4412 1.0000 ans(:,:,5) = 1.0000 1.0000 1.0000 1.0000 1.3090 1.0000 ans(:,:,6) = 1.0000 1.0000 1.0000 1.0000 1.3265 1.0000
That will normalize all rows except the specified row. I should point out that while this method discards the normalized results for one row, it's faster than indexing
  댓글 수: 2
chan
chan 2021년 9월 29일
Thank you it helps me after modifying the code. when i run your code some error occur in this step
A = A./sum(A,2);
DGM
DGM 2021년 9월 30일
Not knowing what was modified or what the error is, I'm going to guess that it's an error about mismatched array dimensions. If that's the case, i'm going to have to guess maybe you're running an older version prior to the introduction to generalized implicit array expansion. If that's the case, you can do this:
%A = A./sum(A,2); % normalize (R2016b or newer)
A = bsxfun(@rdivide,A,sum(A,2)); % normalize (pre-R2016b)
If that's not the case, you'll have to let me know what's been changed and what the specific error message is.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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