Concatenate logical/numerical arrays element wise
Show older comments
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 Comments
James Tursa
on 27 Jul 2022
Edited: James Tursa
on 27 Jul 2022
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
on 28 Jul 2022
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!