Clear Filters
Clear Filters

How to write values in a single array

4 views (last 30 days)
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].

Accepted Answer

Voss
Voss on 5 Jul 2022
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

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!