program to reshape an array and perform some operations.

3 ビュー (過去 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 Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by