Sort vector A according to vector B
2 ビュー (過去 30 日間)
古いコメントを表示
I have two 14x1 vectors A and B. i am trying to sort A to match with B.
A = [0.9; 0.2; 0.4; 0.2; 0.3; 0.4; 0.7; 0.4; 0.1; 0.8; 0; 0; 0; 0];
B = [0; 0; 1; 1; 1; 1; 0; 0; 0; 0; 0; 0; 0; 0];
I am trying to make it so the 0's in A match with the 1's in B with a result of:
C = [0.9; 0.2; 0; 0; 0; 0; 0.4; 0.2; 0.3; 0.4; 0.7; 0.4; 0.1; 0.8]
This is my attempt:
TF1 = B(:,1)==1;
for j = 1:length(A)
n = j:length(B);
A(TF1,:) = 0
end
Any help would be appreciated. Thank you!
2 件のコメント
Image Analyst
2022 年 3 月 27 日
編集済み: Image Analyst
2022 年 3 月 27 日
Why? What's the use case (context, reason)? Is it homework?
Did you try a simple for loop with two counters to keep track of indexes?
採用された回答
Bruno Luong
2022 年 3 月 27 日
A = [0.9; 0.2; 0.4; 0.2; 0.3; 0.4; 0.7; 0.4; 0.1; 0.8; 0; 0; 0; 0];
B = [0; 0; 1; 1; 1; 1; 0; 0; 0; 0; 0; 0; 0; 0];
Ais0=A==0;
Bis1=B==1;
C=zeros(size(A));
C(Bis1)=A(Ais0);
C(~Bis1)=A(~Ais0);
C
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Shifting and Sorting Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!