I have matrix K and Z

7 visualizaciones (últimos 30 días)
jane
jane el 1 de Jul. de 2022
Comentada: KALYAN ACHARJYA el 1 de Jul. de 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

Respuestas (2)

David Hill
David Hill el 1 de Jul. de 2022
s=logical(sum(Z,2));
K(s,:)=0;

KALYAN ACHARJYA
KALYAN ACHARJYA el 1 de Jul. de 2022
Editada: KALYAN ACHARJYA el 1 de Jul. de 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 comentarios
jane
jane el 1 de Jul. de 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 el 1 de Jul. de 2022
It's working in Live Matlab, see the results too.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by