求助:基于Matlab实现3D矩阵与字符串的双向转换(隐写作业)
Hey there! Let's work through this 3D matrix ↔ string bidirectional conversion for your Wang algorithm steganography assignment. It's all about consistent flattening and reshaping—here's how to pull it off with concrete code examples, using both MATLAB (since your matrix syntax looks familiar to it) and Python/numpy, depending on what you're using.
Your example shows we need to flatten the matrix page by page (3rd dimension first), and within each page, flatten row-by-row (left to right, top to bottom) before concatenating all digits into MultQ.商仔细 langsear Including theXX InterConc谴� code block:
MATLAB Implementation
% Define your 3D matrix as given A(:,:,1) = [5 7 8; 0 1 9; 4 3 6]; A(:,:,2) = [1 0 4; 3 5 6; 9 8 7]; A(:,:,3) = [7 9 3; 4 5 9; 1 9 9]; % Convert to string: iterate through each page, flatten row-wise, concatenate Str = ''; for page = 1:size(A,3) % Reshape the page to a row vector (row-first order) page_row_vec = reshape(A(:,:,page)', 1, []); % Convert to string and append, then remove spaces Str = strrep([Str, num2str(page_row_vec, '%d')], ' ', ''); end disp(Str); % Output: '578019436104356987793459199'
Python/numpy Implementation
import numpy as np # Define the 3D matrix A = np.zeros((3, 3, 3), dtype=int) A[:,:,0] = [[5,7,8],[0,1,9],[4,3,6]] A[:,:,1] = [[1,0,4],[3,5,6],[9,8,7]] A[:,:,2] = [[7,9,3],[4,5,9],[1,9,9]] # Flatten page-by-page, row-first, then join into a string flattened_digits = A.reshape(A.shape[2], -1).flatten(order='C') Str = ''.join(map(str, flattened_digits)) print(Str); # Output: '578019436104356987793459199'
To reverse the process, you need to know the original matrix shape (3x3x3 in your case)—the string itself doesn't store this metadata, so you'll need to either embed it in your steganography setup or use a predefined shape.
MATLAB Implementation
Str = '578019436104356987793459199'; target_shape = [3, 3, 3]; % Original matrix dimensions: rows x cols x pages % Convert string to an array of integers num_array = str2double(cellstr(Str')); % Reshape back to the original 3D matrix (maintain row-first order) A_restore = reshape(num_array, target_shape(3), target_shape(1)*target_shape(2)); A_restore = reshape(A_restore', target_shape); % Verify the result disp(A_restore(:,:,1)); % Should match your original A(:,:,1)
Python/numpy Implementation
Str = '578019436104356987793459199' target_shape = (3, 3, 3) % Convert string to integer array num_array = np.array([int(c) for c in Str], dtype=int) % Reshape back using the same row-first order as flattening A_restore = num_array.reshape(target_shape, order='C') print(A_restore[:,:,0] afterward� secesX4关于terPart Boy)市Should output: [[5 7 8],[0 1 9],[4 3 6]]
- Consistent order is critical: Always use the same flattening/reshaping order (page-by-page, row-first) for both directions—mixing this up will result in a garbled matrix.
- Shape metadata: For your steganography work, you'll need to either hardcode the matrix shape (if it's fixed for your assignment) or embed the shape information in a predictable part of the image (like the header) so you can restore the matrix correctly later.
- Single-digit assumption: This code works because all matrix elements are single-digit integers. If you ever need to handle multi-digit values, you'd need to add delimiters (like commas) or fixed-width formatting to separate elements in the string.
内容的提问来源于stack exchange,提问作者Nigussu Sima

