Concatenate logical/numerical arrays element wise

4 ビュー (過去 30 日間)
Aiman Raza
Aiman Raza 2022 年 7 月 27 日
コメント済み: Aiman Raza 2022 年 11 月 16 日
I have five 2D arrays (912384x224) with either a logical 1 or 0 (essentially five logical masks). The five arrays are stored together in a single cell.
I wish to combine the logical arrays such that I have a new cell that combines the 1s and 0s of each array.
For example, if
H{1}(1,1)=1;
H{2}(1,1)=0;
H{3}(1,1)=1;
H{4}(1,1)=1;
H{5}(1,1)=0;
then the new cell array should be:
combinedH{1}(1,1)=[1,0,1,1,0] (or just 10110)
If I do the regular cat function, the arrays do not merge element-wise. Converting the values to string and then back to double after concatenation is memory consuming.
I can obtain the desired result through the below code, but it is also incredibly time-consuming.
for m=1:912384
for n=1:224
logic{m,n}=[Hmask{1}(m,n),Hmask{2}(m,n),Hmask{3}(m,n),Hmask{4}(m,n),Hmask{5}(m,n)];
end
end
Any help would be appreciated. I have tried many examples to concatenate without any success.
Regards
  2 件のコメント
James Tursa
James Tursa 2022 年 7 月 27 日
編集済み: James Tursa 2022 年 7 月 27 日
The main question you should ask yourself is how will this be used downstream in your code? That will determine what the best way to concatenate is. The easiest, of course, is just to concatenate them in the 3rd dimension into a 3D logical array. Another way is to combine them into bits of a 2D int8 matrix. Or perhaps into decimal numbers of a 2D int16 matrix such that the decimal conversion for display shows the 10110 etc. patterns. Bottom line is there are multiple ways to combine them, but you need to tell us how they will be used downstream in your code so we can reasonably advise you on the best way to do it. Is it just for display purposes? Will you still need to get at the original "bits"? Or ...?
Aiman Raza
Aiman Raza 2022 年 7 月 28 日
Thanks for your reply. This was what I was looking for. I will use the concatenated matrix to create a binary sequence, which I will multiply further with a data matrix (double) of the same size.

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

採用された回答

James Tursa
James Tursa 2022 年 7 月 27 日
編集済み: James Tursa 2022 年 7 月 27 日
Here is one way:
% Generate sample data
H{1} = rand(2,3)<0.5;
H{2} = rand(2,3)<0.5;
H{3} = rand(2,3)<0.5;
Hc = cat(3,H{:}) % combined
Hc = 2×3×3 logical array
Hc(:,:,1) = 1 0 0 0 0 1 Hc(:,:,2) = 1 1 0 1 0 1 Hc(:,:,3) = 0 1 0 1 1 0
% Extract a vector of the original data
squeeze(Hc(1,1,:))
ans = 3×1 logical array
1 1 0
squeeze(Hc(1,2,:))
ans = 3×1 logical array
0 1 1
Or you could permute the vectors into the 1st dimension. E.g.,
Hp = permute(Hc,[3,1,2]);
Then extraction is on the 1st dimension and doesn't need squeezing:
Hp(:,1,1)
ans = 3×1 logical array
1 1 0
Hp(:,1,2)
ans = 3×1 logical array
0 1 1
And yet another way if you want row vector output is to permute your vectors into the 2nd dimension. E.g.,
H2 = permute(Hc,[4,3,1,2]);
Then extraction is on the first two dimensions:
H2(:,:,1,1)
ans = 1×3 logical array
1 1 0
H2(:,:,2,1)
ans = 1×3 logical array
0 1 1
  1 件のコメント
Aiman Raza
Aiman Raza 2022 年 11 月 16 日
Thanks a lot for your response!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by