program to reshape an array and perform some operations.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Ronald Niwamanya
el 9 de Mayo de 2021
Comentada: Ronald Niwamanya
el 11 de Mayo de 2021
I have an array, A and would like to perfrom the following operations.
A=[1 0 1 1 0 1 1 0 1 1 1 1 0 1 1 1];
- From A, obtain four arrays. reshape can be used here.
A1= [1 0 1 1];
A2= [0 1 1 0];
A3= [1 1 1 1];
A4= [0 1 1 1];
2. Perform the following permutations to obtain B, C, D, E and F. Note, 1-A1 means inverting A1, 1 becomes 0 and viceversa.
B=[A1 A2 A3 A4];
C=[1-A1 A2 A3 A4];
D=[A1 1-A2 A3 A4];
E=[A1 A2 1-A3 A4];
F=[A1 A2 A3 1-A4];
For B,C,D,E and F. I would like to reshape and perfrom binary to decimal conversion for each.
Forexample.
G=reshape(B,2,2);
G=bi2de(G);
Thank you in advance.
3 comentarios
Jan
el 10 de Mayo de 2021
All we see is the code you have posted, which does exactly, what it is intented to do. I cannot guess relaibly, what "doing it manually" means. Which of the steps do you want to perform automatically?
Respuesta aceptada
DGM
el 10 de Mayo de 2021
Your example is not working and leaves ambiguity. B-F are 1x16 vectors, and you're trying to reshape them to be 2x2. Besides not working, this makes the intent hard to discern. I'm going to assume you mean to break each vector into 2-bit chunks and convert each to decimal. This gives us 8 decimal results per row. I arranged G as a 5x8 array. If the byte order is incorrect, set the appropriate option in the call to bi2de().
A=[1 0 1 1 0 1 1 0 1 1 1 1 0 1 1 1];
A = logical(A);
G = zeros(5,8);
for row = 1:5
B = A;
if row>1
idx = (row-2)*4+(1:4);
B(idx) = ~B(idx);
end
G(row,:) = bi2de(reshape(B.',2,8).');
end
G
gives
G =
1 3 2 1 3 3 2 3
2 0 2 1 3 3 2 3
1 3 1 2 3 3 2 3
1 3 2 1 0 0 2 3
1 3 2 1 3 3 1 0
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!