program to reshape an array and perform some operations.

조회 수: 9 (최근 30일)
Ronald Niwamanya
Ronald Niwamanya 2021년 5월 9일
댓글: Ronald Niwamanya 2021년 5월 11일
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
Ronald Niwamanya
Ronald Niwamanya 2021년 5월 10일
@Jan I need a code that can perfrom the task without doing it manually.
Jan
Jan 2021년 5월 10일
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?

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

채택된 답변

DGM
DGM 2021년 5월 10일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by