Concatenate logical/numerical arrays element wise
20 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
James Tursa
le 27 Juil 2022
Modifié(e) : James Tursa
le 27 Juil 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 ...?
Réponse acceptée
James Tursa
le 27 Juil 2022
Modifié(e) : James Tursa
le 27 Juil 2022
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
% Extract a vector of the original data
squeeze(Hc(1,1,:))
squeeze(Hc(1,2,:))
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)
Hp(:,1,2)
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)
H2(:,:,2,1)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!