我正在使用重塑功能使用矩阵。我有一个矩阵大小990 x 8
。首先,我将其重塑为A x 400
A,确定st 990x8可被400整除。因此,我们必须添加80个零作为填充。所以,A是
(990+10)/400=200
我的新矩阵是20 x 400
。现在,我在A中将一行设置为错误行。第二步,我想用该行错误恢复原始矩阵大小990 x 8。因此,数字行错误将是
400/8=50 rows
当您看到下图时,非常清楚。现在,我想通过matlab代码实现此方案。让我将实现看做是波纹管。但是,它与我的目标并不相似。它在个别行中出现了-1(例如:[0 0 -1 0 0 ..],真正的答案必须是[0 0 0 ...]或[-1 -1 -1 ..],因为如果行中没有错误只会包含0或1,并且错误行仅为-1值)。请解决帮我
bitstream = reshape( orignalPacket.', [],1); %size 990 x 8
%% Add padding
bitstream(end+80)=0; % add zeros padding at the end
%% New matrix 20 x 400
newmatrix= reshape( bitstream.', [],psize);
%% Add one error row
newmatrix(5,:)=-1; %row 5th is error
%% Recovery to orignal packet
bitstream_re = reshape( newmatrix, [],1);
bitstream_re(end-80+1:end)=[];%% Remove padding
matrix_re=reshape(permute(reshape(bitstream_re,size(bitstream_re,2),8,[]),[2 1 3]),8,[]);
matrix_re=matrix_re'; %Recovery matrix-
您遇到的问题是因为MATLAB使用列优先索引,并且通过操作您正在按行索引到矩阵中。
这是修改后的似乎有效的代码-
error_row = 5; %// row index to be set as error row
bitstream = orignalPacket; %// Assuming orignalPacket is of size 990 x 8
bitstream(end+10,:)=0; %// pad with 10 rows of zeros at the end
%// New matrix 20 x 400
newmatrix = reshape(bitstream.',400,20).';
%// Add one error row
newmatrix(error_row,:)=-1; %// row 5th is error
%// Recovery orignal packet
bitstream_re = reshape(newmatrix.',1,[]); %//'
bitstream_re(end-80+1:end)=[]; %// Remove padding
matrix_re = reshape(bitstream_re,8,[]).'; %//'# Recovered matrix
顺便说一句,您可以通过以下方式获得相同的输出,matrix_re
而无需进行所有这些重塑和繁琐的索引操作:
matrix_re = orignalPacket; %// Assuming orignalPacket is of size 990 x 8
error_row = 5; %// row index to be set as error row
matrix_re(50*(error_row-1) + 1 :50*error_row,:) = -1;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句