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

1 view (last 30 days)
[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

Answers (1)

Walter Roberson
Walter Roberson on 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.

Community Treasure Hunt

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

Start Hunting!