I have a matrix that I use it like an index, for example: A=[1 3 2 4 1 3] which points to a matrix which represents binary bits B=[00 01 11 10]. How can I produce these bits (like decimal numbers of course) but in a single line matrix (e.g. 0 0 1 1 0 1 1 0 0 0 1 1)? I mean I want: C=B(1) gives me 0 0, C=B(2) gives me 0 1 and so on...
Thank you..

 채택된 답변

Walter Roberson
Walter Roberson 2011년 11월 13일

0 개 추천

It is not possible to do what you want. There is no data type in MATLAB with which you can supply a single index and get out a vector of numeric values using () subscripting. Cell arrays come close, but when you use () subscripting with them, you get out a cell array that contains the vector, rather than the vector itself. You can use {} subscripting with a cell array to "unwrap" the cell array from the vector:
B={{0 0} {0 1} {1 1} {1 0}};
C = B{1};
But really it is easier to use straight arrays and appropriate indexing:
B = [0 0; 0 1; 1 1; 1 0];
C = B[1,:];

추가 답변 (2개)

bym
bym 2011년 11월 13일

0 개 추천

dec2bin([1:5])
ans =
001
010
011
100
101

댓글 수: 1

athpapa -
athpapa - 2011년 11월 13일
I don't want this. I just want the first matrix A to point in the second matrix B which has binary bits but in demical numbers. B=[00 01 11 10]. And when I type C=B(1) to give me the first two zeros, when type B(2) the zero and one and so on...Do you know how can I do this?

댓글을 달려면 로그인하십시오.

Fangjun Jiang
Fangjun Jiang 2011년 11월 13일

0 개 추천

B=[00 01 11 10];
C=num2str(B','%02d');
or
i=1;
C=sprintf('%02d',B(i));

댓글 수: 2

athpapa -
athpapa - 2011년 11월 13일
Is it possible to do this with integers and not strings?Because I need integers (numbers) and not strings...
Fangjun Jiang
Fangjun Jiang 2011년 11월 13일
After the code above, use str2num([C(:)]), or uint8(str2num([C(:)]))

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by