Why do I get: "Error using vertcat Dimensions of matrices being concatenated are not consistent."

I have a vector, which should be splitted at certain points (if condition). The parts of the vector should emerge as column in a matrix. In a for-loop I want to establish the new matrix step by step. I have made some preallocation, but I still get an error message. Can someone help me? Here is my code:
Entire = zeros (d,e); %preallocation
for b = 1: lengthRS
if trigger (b,1) ==1 && trigger (b+1) == 1; %condition for splitting
single = ERGsignal(b:b+d,1); %make single part of vector (ERGsignal)
Entire = [Entire; single]; %Building up Matrix
end
end

1 Comment

Note that it is a bad idea to name a variable |single, as this is the name of the inbuilt function single.

Sign in to comment.

 Accepted Answer

Does this work ?
Entire = [];
for b = 1: lengthRS
if trigger (b,1) ==1 && trigger (b+1,1) == 1; %condition for splitting
single = ERGsignal(b:b+d,1); %make single part of vector (ERGsignal)
Entire = horzcat(Entire, single); %Building up Matrix
end
end
Best wishes
Torsten.

2 Comments

Note that it is a bad idea to name a variable single, as this is the name of the inbuilt function single.
Thank you so much, Torsten! This was exactly what I was looking for! :)

Sign in to comment.

More Answers (1)

Entire has e columns
single has 1 column
So unless e == 1, they won't match and hence the error. That being said, it looks like maybe you intended to build up Entire by columns instead of rows. If so, use a comma instead of a semi-colon when attaching "single" (and adjust the size of Entire accordingly).
"single" is an extremely poor choice for a variable name btw since it matches a MATLAB class name. I would advise choosing a different name for this variable.

1 Comment

Thank you for your answer. You are right, my variables were translated rather imprudently.

Sign in to comment.

Asked:

on 1 Apr 2015

Commented:

on 2 Apr 2015

Community Treasure Hunt

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

Start Hunting!