Problems with changing the order of a binary representation

조회 수: 5 (최근 30일)
Jennifer Arellana
Jennifer Arellana 2021년 4월 27일
댓글: Clayton Gotberg 2021년 4월 27일
Hello everyone,
I have a problem to organize a binary representation that I calculate in Matlab, like this:
NJ=3
for i = 1:2^NJ-1
struct(i,:) = dec2bin(i,NJ);
end
This gives me this result:
ans:
struct =
7×3 char array
'001'
'010'
'011'
'100'
'101'
'110'
'111'
The problem is that I need this binary representation to have the following order:
100
010
110
001
101
011
111
I really need this to be done by the code because if you increase the number in NJ, it is no longer practical to do the manual change, as the amount of struct data increases exponentially.
I appreciate any help, Thank you.
  댓글 수: 3
Jennifer Arellana
Jennifer Arellana 2021년 4월 27일
Yes, I changed it because I noticed that I had not put the correct data, but I also wanted to try to convert from characters into numbers but when I tried it I got the following result:
>> a = str2double(B_ampl)
a =
Inf
I don't understand why.
Clayton Gotberg
Clayton Gotberg 2021년 4월 27일
what is B_ampl? If it's your replacement for struct, the problem is probably with the formatting of the input. If you input a simple char array, the function assumes you're giving it one number. If you input a string array or a cell array containing char arrays, the function can see the numbers separately.

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

채택된 답변

Clayton Gotberg
Clayton Gotberg 2021년 4월 27일
편집: Clayton Gotberg 2021년 4월 27일
Use the fliplr function to swap the direction of arrays!
NJ=3
for i = 1:2^NJ-1
binary_save(i,:) = fliplr(dec2bin(i,NJ));
end
I also changed the name of struct to binary_save because struct is the name of a built-in MATLAB function, and it's best to avoid accidentally overwriting MATLAB functions. For example,
input = ones(5,4); % A 5x4 matrix
size = 3; % Setting the size for something later
for k = 1:size(input,1)
% Some operation
end
Will run from k = 1:3 instead of k = 1:5 (the number of rows in the input) and
input = magic(5);
size = 3; % Setting the size for something later
for k = 1:size(input,1)
% Some operation
end
Gives an error about how the index in position 1 doesn't make sense but doesn't even tell you in which line it's occured.
  댓글 수: 3
Clayton Gotberg
Clayton Gotberg 2021년 4월 27일
I hope Mathworks will eventually be able to send alerts about new answers to posts you're currently viewing. I've done the same thing at least 3 times this week!
Paul Hoffrichter
Paul Hoffrichter 2021년 4월 27일
I will start hitting "follow" to get timely alerts. Not perfect since emails take time to arrive depending upon the email service.

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

추가 답변 (1개)

Paul Hoffrichter
Paul Hoffrichter 2021년 4월 27일
NJ=4
for i = 1:2^NJ-1
struct(i,:) = fliplr(dec2bin(i,NJ));
end
struct =
15×4 char array
'1000'
'0100'
'1100'
'0010'
'1010'
'0110'
'1110'
'0001'
'1001'
'0101'
'1101'
'0011'
'1011'
'0111'
'1111'

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by