Error using horzcat Dimensions of matrices being concatenated are not consistent.
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Signal=[zeros(1,((Lmax-SignalLength-1)/2)),(Obj.CompCellList{CellNr}{iFrame}.(signal)),zeros(1,((Lmax-SignalLength+1)/2))];
I want to add zeros to both sides of the signal so that its length is equal to the value of Lmax. I have two more conditions where Lmax and SignalLength are either even or odd, but when one of them is even and the other is odd, I need to add different amounts of zeros to either side of the signal. When I add equal amounts of zeros to both sides in the case where both values are either odd or even, I don't have this problem.
Lmax=77
SignalLength=20
SignalLength=length(Obj.CompCellList{CellNr}{iFrame}.(signal))
0 Commenti
Risposta accettata
Voss
il 22 Set 2022
Here's how you can add zeros to the beginning and end of your signal (with the extra zero going to the end in case the number of zeros needed is odd - as you appear to have it):
% Signal = Obj.CompCellList{CellNr}{iFrame}.(signal);
Signal = rand(1,4);
Lmax = 9;
SignalLength = length(Signal);
Nzeros = Lmax-SignalLength;
if mod(Nzeros,2)
Signal = [zeros(1,(Nzeros-1)/2), Signal, zeros(1,(Nzeros+1)/2)];
else
Signal = [zeros(1,Nzeros/2), Signal, zeros(1,Nzeros/2)];
end
disp(Signal);
Or, using a variable Nzeros_mod2 = mod(Nzeros,2) instead of the if/else construction:
Signal = rand(1,4);
Lmax = 9;
SignalLength = length(Signal);
Nzeros = Lmax-SignalLength;
Nzeros_mod2 = mod(Nzeros,2);
Signal = [zeros(1,(Nzeros-Nzeros_mod2)/2), Signal, zeros(1,(Nzeros+Nzeros_mod2)/2)];
disp(Signal);
Another example, where the number of zeros is even:
Signal = rand(1,4);
Lmax = 8;
SignalLength = length(Signal);
Nzeros = Lmax-SignalLength;
Nzeros_mod2 = mod(Nzeros,2);
Signal = [zeros(1,(Nzeros-Nzeros_mod2)/2), Signal, zeros(1,(Nzeros+Nzeros_mod2)/2)];
disp(Signal);
Notice that your expression will work only when the number of zeros is odd, i.e., when one of Lmax and SignalLength is odd and the other is even. In case they are both even or both odd, then their difference is even and you'd get an error using the zeros function because (Lmax-SignalLength+1)/2 is not an integer:
Signal = rand(1,4);
Lmax = 8;
SignalLength = length(Signal);
try
zeros(1,(Lmax-SignalLength+1)/2) % error here
catch ME
disp(ME.message); % report the error message
end
However, this is not the error message you report in the title of the question. The error message, "Dimensions of matrices being concatenated are not consistent." in this context would suggest that your signal is not of size 1 in dimension 1 most likely:
Signal = rand(2,4); % size 2 in dimension 1, i.e., Signal has two rows
Lmax = 8;
SignalLength = length(Signal);
Nzeros = Lmax-SignalLength;
Nzeros_mod2 = mod(Nzeros,2);
try
Signal = [zeros(1,(Nzeros-Nzeros_mod2)/2), Signal, zeros(1,(Nzeros+Nzeros_mod2)/2)]; % error here
catch ME
disp(ME.message); % report the error message
end
To add zeros to the beginning and end of a signal with size other than 1 in dimension 1, take that into account in calling the zeros function:
Signal = rand(2,4);
Nrows = size(Signal,1); % number of rows of Signal
Lmax = 8;
SignalLength = size(Signal,2); % use size(_,2) instead of length() for robustness
Nzeros = Lmax-SignalLength;
Nzeros_mod2 = mod(Nzeros,2);
Signal = [zeros(Nrows,(Nzeros-Nzeros_mod2)/2), Signal, zeros(Nrows,(Nzeros+Nzeros_mod2)/2)];
disp(Signal);
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Measurements and Feature Extraction 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!