Azzera filtri
Azzera filtri

How to construct matrix based on order of input?

3 visualizzazioni (ultimi 30 giorni)
I am having 7 random binary input numbers. Now, I have to construct a matrix with rows indicating correct order of input and columns indicating order in which the inputs are available in original sequence. Number '1' is assigned to original order of input and remaining entries in row are assigned '0'. For example binary number 001 is at 6th position in original input order, so in matrix at 6th column of 1st row '1' is assigned and remaining entries are assigned '0'.
Example:
A = 110; 101; 011; 111; 100; 001; 010
Expected matrix:
Column
Row 1 2 3 4 5 6 7
001 0 0 0 0 0 1 0
010 0 0 0 0 0 0 1
011 0 0 1 0 0 0 0
100 0 0 0 0 1 0 0
101 0 1 0 0 0 0 0
110 1 0 0 0 0 0 0
111 0 0 0 1 0 0 0

Risposta accettata

Image Analyst
Image Analyst il 12 Dic 2022
Try this, as long as it's not your homework, in which case you can't use it or you'd risk getting into trouble with your instructor.
A = [110; 101; 011; 111; 100; 001; 010]; % 6 5 3 7 4 1 2
expectedMatrix = zeros(size(A, 1), 7);
for row = 1 : size(A, 1)
str = sprintf('%3.3d', A(row));
col = bin2dec(str);
expectedMatrix(row, col) = 1;
end
% Show in command window:
expectedMatrix
expectedMatrix = 7×7
0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by