How do I map array values on a logical array

6 ビュー (過去 30 日間)
Riccardo Micci
Riccardo Micci 2021 年 1 月 12 日
コメント済み: Steve Eddins 2021 年 1 月 12 日
Hello,
I have a vector with numbers that i need to be remapped on different positions according to a logical bitmap array.
Example:
A = [3 6 8 1 0 -3];
B = [ 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0];
Result = [ 0 0 0 0 0 0 0 0 3 6 8 1 0 -3 0 0 0];
The number of ones in B always matches the length of A but in general length(B)~=length(A)
How can i do that without going through a for loop?
Thanks!

採用された回答

Steve Eddins
Steve Eddins 2021 年 1 月 12 日
C = zeros(size(B));
C(logical(B)) = A
  2 件のコメント
Riccardo Micci
Riccardo Micci 2021 年 1 月 12 日
Thanks it's a very elegant solution!
Steve Eddins
Steve Eddins 2021 年 1 月 12 日
In addition to the doc link that provided, see my 28-Jan-2008 blog post on the topic.

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

その他の回答 (1 件)

Bruno Luong
Bruno Luong 2021 年 1 月 12 日
編集済み: Bruno Luong 2021 年 1 月 12 日
Result = zeros(size(B));
Result(B==1)=A

カテゴリ

Help Center および File ExchangeExponents and Logarithms についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by