I have matrix K and Z

5 vues (au cours des 30 derniers jours)
jane
jane le 1 Juil 2022
Commenté : KALYAN ACHARJYA le 1 Juil 2022
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

Réponses (2)

David Hill
David Hill le 1 Juil 2022
s=logical(sum(Z,2));
K(s,:)=0;

KALYAN ACHARJYA
KALYAN ACHARJYA le 1 Juil 2022
Modifié(e) : KALYAN ACHARJYA le 1 Juil 2022
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 commentaires
jane
jane le 1 Juil 2022
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 le 1 Juil 2022
It's working in Live Matlab, see the results too.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by