program to reshape an array and perform some operations.

1 view (last 30 days)
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];
  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 Comments
Ronald Niwamanya
Ronald Niwamanya on 10 May 2021
@Jan I need a code that can perfrom the task without doing it manually.
Jan
Jan on 10 May 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?

Sign in to comment.

Accepted Answer

DGM
DGM on 10 May 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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!