1D series to 2D stacked

1 回表示 (過去 30 日間)
Eduardo Santos
Eduardo Santos 2018 年 11 月 26 日
コメント済み: Eduardo Santos 2018 年 11 月 27 日
Hi,
I`m trying to think in a best option to "convert" a 1d arrays to a 2d as follows:
B has a fixed size of 10000x(a prior know number, here 6), so it just need to stack number 1's regarding the number into the aray A.
A =
1 5 3 1 2 4
B =
....
....
....
0 1 0 0 0 0
0 1 0 0 0 1
0 1 1 0 0 1
0 1 1 0 1 1
1 1 1 1 1 1 % ROW 1
It can be done maniulating the arrays in a slow way, I would like to do much faster.
Thanks
  2 件のコメント
Kevin Chng
Kevin Chng 2018 年 11 月 27 日
Hi,
to stack number 1's regarding the number into the aray A?Do you mind to elaborate the relationship between A and B?
Walter Roberson
Walter Roberson 2018 年 11 月 27 日
Kevin, read the output B from the bottom upwards. The first column has one 1, which is the same number as A(1). The second column has five 1, which is the same number as A(2). And so on. So if you were to sum(B) then you would get A, with it always being the last so-many entries that are set to 1.

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

採用された回答

Stephen23
Stephen23 2018 年 11 月 27 日
編集済み: Stephen23 2018 年 11 月 27 日
In one line:
B = A>=(10000:-1:1).'
Or for versions prior to R2016b:
B = bsxfun(@ge,A,(10000:-1:1).');
  2 件のコメント
Walter Roberson
Walter Roberson 2018 年 11 月 27 日
Note: This requires R2016b or later
Eduardo Santos
Eduardo Santos 2018 年 11 月 27 日
Thanks!

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 11 月 27 日
t = fliplr(1 : 10000);
B = t.' <= A;
This requires R2016b or later. For earlier releases, change the B line to
B = bsxfun(@le, t.', A);
  1 件のコメント
Eduardo Santos
Eduardo Santos 2018 年 11 月 27 日
Thanks!

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by