# Error using horzcat Dimensions of matrices being concatenated are not consistent.

1 Ansicht (letzte 30 Tage)
C A am 22 Sep. 2022
Beantwortet: Voss am 22 Sep. 2022
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 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Voss am 22 Sep. 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);
0 0 0.8021 0.0218 0.9967 0.1352 0 0 0
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);
0 0 0.0974 0.4638 0.7127 0.5242 0 0 0
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);
0 0 0.7752 0.4344 0.5274 0.7130 0 0
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
Size inputs must be integers.
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
Dimensions of arrays being concatenated are not consistent.
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 0 0.4438 0.8402 0.1166 0.8648 0 0 0 0 0.2582 0.4988 0.6103 0.5558 0 0
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Find more on Linear Algebra in Help Center and File Exchange

R2017b

### Community Treasure Hunt

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

Start Hunting!