How to write values in a single array

조회 수: 5 (최근 30일)
lilly lord
lilly lord 2022년 7월 5일
댓글: lilly lord 2022년 7월 6일
a=[11 23 165 200 213];
a1=de2bi(a,8,'left-msb');
out1=[];
for i=1:length(a)
k= mat2cell(a1(i,:),1,[4,4]);
k1=bi2de(cell2mat(k(1)))
k2=bi2de(cell2mat(k(2)))
out1(i,:)=[k2 k1]
end
output
out1 =
13 0
14 8
10 5
1 3
10 11
a1 is
0 0 0 0 1 0 1 1
0 0 0 1 0 1 1 1
1 0 1 0 0 1 0 1
1 1 0 0 1 0 0 0
1 1 0 1 0 1 0 1
k1 and k2 are not in left-msb
I am facing two problems.
1) since left-msb is used in 2nd line(no problem here) but when I split it and convert it into decimals I am getting half left-msb and half right-msb.
2) Secon I want to write the output in a single row like [13 0 14 8 10 5 1 3 10 11].
  댓글 수: 1
Walter Roberson
Walter Roberson 2022년 7월 5일
2.
%to convert out1 to row form
out1 = reshape(out1.', 1, []);

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

채택된 답변

Voss
Voss 2022년 7월 5일
Here's a guess at what result you want to see for problem 1:
a=[11 23 165 200 213];
a1=de2bi(a,8,'left-msb');
out1=[];
for i=1:length(a)
k= mat2cell(a1(i,:),1,[4,4]);
k1=bi2de(cell2mat(k(1)),'left-msb'); % make k1 and k2 left-msb like a1 is?
k2=bi2de(cell2mat(k(2)),'left-msb');
out1(i,:)=[k2 k1];
end
out1 = reshape(out1.',1,[])
out1 = 1×10
11 0 7 1 5 10 8 12 5 13
Another way to do the same:
a=[11 23 165 200 213];
a1 = dec2bin(a,8) - '0';
n = numel(a);
out1 = zeros(n,2);
for i = 1:n
out1(i,2) = polyval(a1(i,1:4),2);
out1(i,1) = polyval(a1(i,5:8),2);
end
out1 = reshape(out1.',1,[])
out1 = 1×10
11 0 7 1 5 10 8 12 5 13

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by