how to change every value not equal to X in every rows

1 回表示 (過去 30 日間)
Isabelle Bouret
Isabelle Bouret 2019 年 4 月 17 日
コメント済み: Walter Roberson 2019 年 4 月 17 日
Hi,
I have a matrix (20000x365) and I have a vector(20000x1) in which I have the column assiciate with the number that I DON'T want to change (This number change for every rows) .
For every rows I want to change every colums, beside the one that is in the vector, for 0
EX: matrix [18 3 1 11; 8 10 11 3; 9 14 6 1; 4 3 15 21]
Vector [3,1,3,2]
The answers should be [0 0 1 0; 8 0 0 0; 0 0 6 0; 0 3 0 0]
Thanks for your help!

採用された回答

madhan ravi
madhan ravi 2019 年 4 月 17 日
編集済み: madhan ravi 2019 年 4 月 17 日
Wanted = zeros(size(matrix));
indices=sub2ind(size(matrix),1:size(matrix,1),Vector);
Wanted(indices)=matrix(indices)
  6 件のコメント
Isabelle Bouret
Isabelle Bouret 2019 年 4 月 17 日
Thank you!!
I have to modified a llittle bit
Wanted = zeros(size(l_s));
for i=1:L
if Vector(i,:)==0
continue
else
indices=sub2ind(size(Matrice),i,Vector(i,:).');
Wanted(indices)=Matrice(indices);
end
end
This work perfectly event though it may not be efficient
Walter Roberson
Walter Roberson 2019 年 4 月 17 日
If you are going to loop then
Wanted = zeros(size(l_s));
for i = 1:L
if Vector(i) ~= 0
Wanted(i, Vector(i)) = Matrice(i, Vector(i));
end
end
or
Wanted = zeros(size(l_s));
for i = find(Vector)
Wanted(i, Vector(i)) = Matrice(i, Vector(i));
end

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 4 月 17 日
編集済み: Walter Roberson 2019 年 4 月 17 日
rows = size(EX_matrix,1);
ind = (1:rows).' + (Vector(:)-1)*rows;
Output = zeros(size(EX_matrix));
Output(ind) = EX_matrix(ind);

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by