What is causing the reshaping error?

1 visualizzazione (ultimi 30 giorni)
Calvin Chang
Calvin Chang il 8 Lug 2019
Modificato: Adam Danz il 11 Lug 2019
I was playing with the number n, and sometime reshape gives an error when it is too small? Can somone help me deduce what is actually going on here?
M = 64;
k = log2(M);
n = 128;
numSamplesPerSymbol = 1;
rng default
dataIn = randi([0 1], n, 1);
stem(dataIn(1:128), 'filled');
title('Random Bits');
xlabel('Bit Index');
ylabel('Binary Value');
dataInMatrix = reshape(dataIn, length(dataIn)/k, k);
dataSymbolsIn = bi2de(dataInMatrix);
% dataSymbolsIn = b2d(dataInMatrix);
figure;
stem(dataSymbolsIn(1:50));
title('RandomYmbols');
xlabel('Symbol Index');
ylabel('Integer Value');

Risposta accettata

Adam Danz
Adam Danz il 9 Lug 2019
Modificato: Adam Danz il 11 Lug 2019
As described in the documentation, reshape() the 2nd and 3rd inputs should be integers and when those two integers are multiplied, it should equal the number of elements in the first input. That makes sense since the 2nd and 3rd inputs define the dimensions of a matrix.
In the case of your example, length(dataIn)/k equals 21.333 so you're asking for a matrix with 21.3 rows which doesn't make sense.
For your script to work, n must be a multiple of k and k must be an integer.
But there are additional areas of concern. For example, log2() is used to define k and that may not return an interger. Secondly, in the line that calls stem(), you hard-coded 1:128 so if you use n=126, you'll get an indexing error at that line. Likewise, in the second call to stem(), you've hard-coded 1:50 which would cause another index error.
Instead of hard coding these indices, you 1:numel(...) or 1:length(...).

Più risposte (0)

Categorie

Scopri di più su Interpolation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by