Combining elements in an array

3 ビュー (過去 30 日間)
sushma sharma
sushma sharma 2016 年 7 月 12 日
回答済み: Star Strider 2016 年 7 月 12 日
Hi, I have a logical array, A = [1 0 1]. How can I combine the elements horizontally, so that I have B = [101]? Any help would be appreciated! SS

採用された回答

Star Strider
Star Strider 2016 年 7 月 12 日
To turn your logical array into a double array, any mathematical operation on it will do the conversion. Here I used ‘+’:
A = logical([1 0 1])
An = +A
whos A*
A =
1 0 1
An =
1 0 1
Name Size Bytes Class Attributes
A 1x3 3 logical
An 1x3 24 double

その他の回答 (1 件)

James Tursa
James Tursa 2016 年 7 月 12 日
編集済み: James Tursa 2016 年 7 月 12 日
Assuming you intend to combine the individual digits into a single decimal number:
B = sum(A.*(10.^(numel(A)-1:-1:0)));
If you intended to make a string instead, then
B = char(A+'0');

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by