How to use indices of one matrix as entries of another matrix?

3 次查看(过去 30 天)
Hello, I have two matrices, M1 ans M2. They both have rows and columns fom 0 to 9 and a to f (16 * 16) matrix.
Mi matrix is filled with random numbers form 0 to 255 in hex decimal notation.
The second matrix M2 is empty but has the same row and column indexs. I need to fill M2 with the help of M1. For eample, in M2, at row 3 and colm 4 ( 34,the first entry of M1) will be 00 ( index of the first entry of M1). In M2 at row 6 col A, the entry will be 01, at row 2 col 9, the entry will be 02, and so on.
%% % Initialize M2 with zeros
M2 = zeros(16,16);
for row = 0:15
for col = 0:15
% get the value from M1
value= M1(row+1,col+1);
% Convert the values to row and col indices for M2
hexStr=dec2hex(value,2); % convert to 2 digit hex
M2_row=hex2dec(hexStr(1))+1;% convert first digit to decimal
M2_col=hex2dec(hexStr(2))+1;% convert first digit to decimal
% %Debugging
% % place the original rows and columns in M2
%
M2(M2_row, M2_col)=row*16+col;
end
end
M2;
If anyone can help.
  1 个评论
Umar
Umar 2024-8-1

Hi @lilly lord,

To help accomplish your task,I first generated random values in matrix M1 using the randi function as an example. Then, initialized matrix M2 with zeros of the same size as M1 and iterated over each element in M1 by converting the value to a two-digit hexadecimal string, then convert it to an index value. Finally, assigned the index value to the corresponding position in M2. Here is example code snippet,

% Define the size of the matrices

rows = 16;

cols = 16;

% Generate random values in M1

M1 = randi([0 255], rows, cols);

% Initialize M2 with zeros

M2 = zeros(rows, cols);

% Fill M2 based on the values in M1

for i = 1:rows

    for j = 1:cols
        % Convert the value in M1 to a two-digit hexadecimal string
        hexValue = dec2hex(M1(i, j), 2);
        % Convert the hexadecimal string to the corresponding index in M2
        index = hex2dec(hexValue) + 1;
        % Assign the index value to M2 at the corresponding position
        M2(i, j) = index;
    end

end

% Display the transformed matrix M2

disp(M2);

Please see attached results.

Feel free to adjust the code to suit any specific requirements or variations in the transformation process. Please let me know if you have any further questions. If this answers your question, please hit accept answer.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2024-8-1
Fake data:
M1 = reshape(randperm(256)-1,16,16);
Y = find(M1==hex2dec('34'));
M1([1,Y]) = M1([Y,1])
M1 = 16x16
52 29 193 93 46 235 149 60 96 230 154 113 101 57 141 11 33 200 20 142 44 254 167 12 69 173 207 213 65 237 43 83 196 201 98 6 13 48 145 41 89 82 94 176 118 138 132 250 59 100 198 253 15 28 187 130 170 135 144 204 109 148 21 190 192 104 179 71 229 23 128 95 189 199 203 63 112 210 147 239 56 162 181 119 110 61 174 155 163 36 74 238 47 252 208 34 218 255 62 241 50 151 125 139 99 209 42 220 114 180 121 19 246 224 37 159 51 54 97 188 205 24 242 1 243 116 22 120 129 217 191 31 216 53 115 178 4 165 211 68 161 171 25 3 157 226 26 127 7 169 123 221 183 117 233 67 249 90 80 39
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Convert:
C = 1+mod(M1.',16);
R = 1+fix(M1.'/16);
S = size(M1);
X = sub2ind(S,C,R);
M2 = nan(S);
M2(X) = 0:255;
M2 = M2.'
M2 = 16x16
188 123 224 143 136 250 35 148 177 207 227 15 23 36 163 52 232 239 180 111 18 62 126 69 121 142 146 201 53 1 219 131 245 16 95 212 89 114 238 159 174 39 106 30 20 187 4 92 37 249 100 116 0 133 117 251 80 13 236 48 7 85 98 75 221 28 214 155 139 24 200 67 235 179 90 226 172 182 237 167 158 161 41 31 171 228 199 255 209 40 157 240 222 3 42 71 8 118 34 104 49 12 189 192 65 211 184 229 170 60 84 175 76 11 108 134 125 153 44 83 127 110 252 150 205 102 242 147 70 128 55 160 46 213 190 57 195 162 45 103 197 14 19 198 58 38 193 78 61 6 173 101 191 165 10 87 216 144 234 115
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Check:
C1 = reshape(cellstr(dec2hex(M1)),S)
C1 = 16x16 cell array
{'34'} {'1D'} {'C1'} {'5D'} {'2E'} {'EB'} {'95'} {'3C'} {'60'} {'E6'} {'9A'} {'71'} {'65'} {'39'} {'8D'} {'0B'} {'21'} {'C8'} {'14'} {'8E'} {'2C'} {'FE'} {'A7'} {'0C'} {'45'} {'AD'} {'CF'} {'D5'} {'41'} {'ED'} {'2B'} {'53'} {'C4'} {'C9'} {'62'} {'06'} {'0D'} {'30'} {'91'} {'29'} {'59'} {'52'} {'5E'} {'B0'} {'76'} {'8A'} {'84'} {'FA'} {'3B'} {'64'} {'C6'} {'FD'} {'0F'} {'1C'} {'BB'} {'82'} {'AA'} {'87'} {'90'} {'CC'} {'6D'} {'94'} {'15'} {'BE'} {'C0'} {'68'} {'B3'} {'47'} {'E5'} {'17'} {'80'} {'5F'} {'BD'} {'C7'} {'CB'} {'3F'} {'70'} {'D2'} {'93'} {'EF'} {'38'} {'A2'} {'B5'} {'77'} {'6E'} {'3D'} {'AE'} {'9B'} {'A3'} {'24'} {'4A'} {'EE'} {'2F'} {'FC'} {'D0'} {'22'} {'DA'} {'FF'} {'3E'} {'F1'} {'32'} {'97'} {'7D'} {'8B'} {'63'} {'D1'} {'2A'} {'DC'} {'72'} {'B4'} {'79'} {'13'} {'F6'} {'E0'} {'25'} {'9F'} {'33'} {'36'} {'61'} {'BC'} {'CD'} {'18'} {'F2'} {'01'} {'F3'} {'74'} {'16'} {'78'} {'81'} {'D9'} {'BF'} {'1F'} {'D8'} {'35'} {'73'} {'B2'} {'04'} {'A5'} {'D3'} {'44'} {'A1'} {'AB'} {'19'} {'03'} {'9D'} {'E2'} {'1A'} {'7F'} {'07'} {'A9'} {'7B'} {'DD'} {'B7'} {'75'} {'E9'} {'43'} {'F9'} {'5A'} {'50'} {'27'} {'83'} {'51'} {'89'} {'0E'} {'DE'} {'99'} {'F8'} {'4F'} {'E7'} {'A6'} {'6C'} {'54'} {'4C'} {'96'} {'28'} {'6F'} {'A4'} {'08'} {'AC'} {'49'} {'12'} {'A8'} {'4D'} {'A0'} {'6A'} {'CA'} {'BA'} {'2D'} {'00'} {'66'} {'86'} {'98'} {'67'} {'92'} {'B6'} {'88'} {'C3'} {'8C'} {'8F'} {'56'} {'46'} {'1B'} {'E8'} {'C5'} {'B8'} {'7C'} {'E4'} {'09'} {'C2'} {'58'} {'F4'} {'69'} {'23'} {'85'} {'42'} {'E1'} {'9C'} {'B1'} {'FB'} {'1E'} {'F5'} {'40'} {'5C'} {'D4'} {'02'} {'B9'} {'4B'} {'0A'} {'55'} {'6B'} {'EA'} {'E3'} {'10'} {'D6'} {'9E'} {'48'} {'3A'} {'4E'} {'26'} {'11'} {'5B'} {'F0'} {'7E'} {'AF'} {'D7'} {'20'} {'CE'} {'DF'} {'EC'} {'31'} {'05'} {'37'} {'7A'} {'F7'} {'DB'} {'57'}
C2 = reshape(cellstr(dec2hex(M2)),S)
C2 = 16x16 cell array
{'BC'} {'7B'} {'E0'} {'8F'} {'88'} {'FA'} {'23'} {'94'} {'B1'} {'CF'} {'E3'} {'0F'} {'17'} {'24'} {'A3'} {'34'} {'E8'} {'EF'} {'B4'} {'6F'} {'12'} {'3E'} {'7E'} {'45'} {'79'} {'8E'} {'92'} {'C9'} {'35'} {'01'} {'DB'} {'83'} {'F5'} {'10'} {'5F'} {'D4'} {'59'} {'72'} {'EE'} {'9F'} {'AE'} {'27'} {'6A'} {'1E'} {'14'} {'BB'} {'04'} {'5C'} {'25'} {'F9'} {'64'} {'74'} {'00'} {'85'} {'75'} {'FB'} {'50'} {'0D'} {'EC'} {'30'} {'07'} {'55'} {'62'} {'4B'} {'DD'} {'1C'} {'D6'} {'9B'} {'8B'} {'18'} {'C8'} {'43'} {'EB'} {'B3'} {'5A'} {'E2'} {'AC'} {'B6'} {'ED'} {'A7'} {'9E'} {'A1'} {'29'} {'1F'} {'AB'} {'E4'} {'C7'} {'FF'} {'D1'} {'28'} {'9D'} {'F0'} {'DE'} {'03'} {'2A'} {'47'} {'08'} {'76'} {'22'} {'68'} {'31'} {'0C'} {'BD'} {'C0'} {'41'} {'D3'} {'B8'} {'E5'} {'AA'} {'3C'} {'54'} {'AF'} {'4C'} {'0B'} {'6C'} {'86'} {'7D'} {'99'} {'2C'} {'53'} {'7F'} {'6E'} {'FC'} {'96'} {'CD'} {'66'} {'F2'} {'93'} {'46'} {'80'} {'37'} {'A0'} {'2E'} {'D5'} {'BE'} {'39'} {'C3'} {'A2'} {'2D'} {'67'} {'C5'} {'0E'} {'13'} {'C6'} {'3A'} {'26'} {'C1'} {'4E'} {'3D'} {'06'} {'AD'} {'65'} {'BF'} {'A5'} {'0A'} {'57'} {'D8'} {'90'} {'EA'} {'73'} {'B7'} {'8C'} {'51'} {'58'} {'B0'} {'89'} {'A9'} {'16'} {'B5'} {'95'} {'38'} {'8D'} {'B2'} {'19'} {'56'} {'F3'} {'2B'} {'D9'} {'87'} {'42'} {'6D'} {'52'} {'C2'} {'98'} {'CC'} {'E1'} {'BA'} {'36'} {'77'} {'48'} {'3F'} {'82'} {'40'} {'02'} {'D0'} {'C4'} {'20'} {'CB'} {'32'} {'49'} {'11'} {'21'} {'B9'} {'4A'} {'3B'} {'78'} {'F6'} {'1A'} {'5E'} {'69'} {'4D'} {'8A'} {'DF'} {'1B'} {'E9'} {'F4'} {'84'} {'81'} {'60'} {'FE'} {'6B'} {'97'} {'A4'} {'F7'} {'71'} {'D7'} {'91'} {'E7'} {'CE'} {'44'} {'09'} {'A8'} {'CA'} {'9A'} {'E6'} {'05'} {'F8'} {'1D'} {'5B'} {'4F'} {'F1'} {'63'} {'7A'} {'7C'} {'D2'} {'DC'} {'70'} {'FD'} {'A6'} {'9C'} {'2F'} {'DA'} {'5D'} {'33'} {'15'} {'61'}

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by