I have matrix K and Z

3 Ansichten (letzte 30 Tage)
jane
jane am 1 Jul. 2022
Kommentiert: KALYAN ACHARJYA am 1 Jul. 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

Antworten (2)

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

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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by