2

In Matlab, how can I merge two bits from two columns (each column consist of one bit) into one column, for example:

X = [1,0;0,1;1,1;0,0;1,1;0,0]

The desired result is:

X = [10;01;11;00;11;00]
3
  • 1
    What is your data type? Double? Commented Aug 20, 2015 at 15:01
  • 3
    Please clarify on what the data type of X is. What you are asking for can't be done with just double arrays. For example, the 01 will be displayed as 1 and the 00 will be displayed as 0. Commented Aug 20, 2015 at 15:04
  • @Ander Biguri , Yes the data is Double, and I want to make it as 1x1 array. Commented Aug 21, 2015 at 11:40

2 Answers 2

2

An easy solution would be:

b = char(X+48)

If the char array is inconvenient, you can transform it to a cell array:

bcell = cellstr(b)
Sign up to request clarification or add additional context in comments.

1 Comment

Of course it is a matter of personal preference, but I find char(X+'0') more easy to read.
1

I may be thinking too easy here, but what about this:

10*X(:,1)+X(:,2)

Note that it will not actually show things like 01 as this is simplified to 1.

If you really want to show 01, you may need to process it as text:

Y=num2str(X)
Y(:,[1 end])

2 Comments

Both methods work for X as decimal and as logical, but you will want to pay attention to the type of output that you need.
@othmannoor Good to hear that! If you want to indicate that you have found what you are looking for you can tick the mark left of the answer that you want to accept.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.