I have matrix K and Z

5 次查看(过去 30 天)
jane
jane 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