How to write values in a single array

6 visualizaciones (últimos 30 días)
lilly lord
lilly lord el 5 de Jul. de 2022
Comentada: lilly lord el 6 de Jul. de 2022
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 comentario
Walter Roberson
Walter Roberson el 5 de Jul. de 2022
2.
%to convert out1 to row form
out1 = reshape(out1.', 1, []);

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 5 de Jul. de 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

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by