program to reshape an array and perform some operations.
조회 수: 9 (최근 30일)
이전 댓글 표시
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
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
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 Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!