I have matrix K and Z

조회 수: 7 (최근 30일)
jane
jane 2022년 7월 1일
댓글: KALYAN ACHARJYA 2022년 7월 1일
I have matrix K and Z
K = [[ 12, 50, 15, 99, 61, 74 ,71],
[54, 23, 14, 13, 16, 89,67],
[12, 45, 78, 90, 12, 56, 16]].
Z = [[ 0, 1, 0, 0, 0, 0.1],
[ 0, 0, 0, 0 , 0, 0, 0]],
[ 0, 0 , 1, 0 , 0, 0,0]],
How to make the value of the first row in the K matrix be 0 if the Z matrix contains the number 1.
for example in the first row in the Z matrix there is a value of 1, how do you make the values in the first row (12, 50, 15, 99, 61, 74 ,71) of the K matrix become ( 0, 0, 0, 0, 0, 0 , 0). Thanks

답변 (2개)

David Hill
David Hill 2022년 7월 1일
s=logical(sum(Z,2));
K(s,:)=0;

KALYAN ACHARJYA
KALYAN ACHARJYA 2022년 7월 1일
편집: KALYAN ACHARJYA 2022년 7월 1일
Another way:
K = {[12, 50, 15, 99, 61, 74 ,71];[54, 23, 14, 13, 16, 89,67];[12, 45, 78, 90, 12, 56, 16]};
Z = {[ 0, 1, 0, 0, 0, 0.1];[ 0, 0, 0, 0 , 0, 0, 0];[ 0, 0 , 1, 0 , 0, 0,0]};
for i=1:length(Z)
if any( Z{i}==1)
dat=length(K{i});
K{i}= zeros(1,dat);
end
end
K
K = 3×1 cell array
{[ 0 0 0 0 0 0 0]} {[54 23 14 13 16 89 67]} {[ 0 0 0 0 0 0 0]}
  댓글 수: 2
jane
jane 2022년 7월 1일
sorry but this doesn't work.
the text in the command window looks like the following "brace indexing is not supported for this type of variable." for if any(z{i}==1)
KALYAN ACHARJYA
KALYAN ACHARJYA 2022년 7월 1일
It's working in Live Matlab, see the results too.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by