Effacer les filtres
Effacer les filtres

hi, I have a cell array consisting binary values and empty cells like below:

2 vues (au cours des 30 derniers jours)
nafila aytija
nafila aytija le 28 Oct 2017
[0] [] [0 0 1 ] []
[0] [] [0 1 0 ] [ 0 0 1]
[0] [] [] [0 0 1]
[0] [0 0 0] [1 1 0] []
I want to do binary addition for the first two which results in [0 0 1]. Any hint/help would be much appreciated. TIA I want to

Réponses (1)

Walter Roberson
Walter Roberson le 28 Oct 2017
L = cellfun(@length, YourCell(:));
maxL = max(L(:));
Pad = @(V) [zeros(1, maxL + 1 - length(V)), V];
padCell = cellfun(Pad, YourCell, 'uniform', 0);
Now all of the entries in padCell are the same size, and you can go ahead with your binary addition between whichever entries you want. The size that was created was one larger than the existing maximum size, because when you add two binary numbers you could potentially get an output that was one larger: with the extra digit of 0 added for everything, the sum of two entries can never overflow the allocated space.
At the end, you can run through the results and unpad: for each entry, keep the maximum of the two original lengths (which were recorded in L), and one more than that if the digit before that became a 1.

Catégories

En savoir plus sur Logical 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!

Translated by