# for loop with vectors having different sizes

54 views (last 30 days)
Gaetano Pavone on 11 Feb 2021
Edited: Bjorn Gustavsson on 12 Feb 2021
I have the following problem:
a=[1 2];
b=[3 4 5];
c=[6 7];
d=[8 9 10 11];
casetype='trial';
for activevector=[a' b']
for passivevector=[c' d']
casestudy=[casetype,' - active (',num2str(activevector'),') passive (',num2str(passivevector'),')'];
mkdir([name,casestudy]);
end
end
Error massage is:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in Untitled (line 9)
for passivevector=[c' d']
For example, I want to have:
casetype - active (1 2) passive (6 7)
casetype - active (1 2) passive (8 9 10 11)
ecc..
Not:
casetype - active (1) passive (6)
casetype - active (1) passive (7)
ecc..
How can I overcome this problem?
Stephen Cobeldick on 12 Feb 2021
Your explanation contradicts the code itself. You wrote that you want "to iterate over the values of a single vector", but the loop iterator variables are named activevector and passivevector, which tells us that you expect them to be vectors.
So which should they be: vectors (as the variable names state) or individual values (as you explained) ?
If you want to iterate over the individual values, remove all transposes inside the concatenations.
Tip: replace the string concatenation with sprintf:
a = 2;
p = 6;
c = 'trial';
sprintf('%s - active (%d) passive (%d)',c,a,p)
ans = 'trial - active (2) passive (6)'

Jan on 12 Feb 2021
Edited: Jan on 12 Feb 2021
Omit the single quotes, because there is no reason for the conjugate complex transposition:
a=[1 2];
b=[3 4 5];
c=[6 7];
d=[8 9 10 11];
casetype='trial';
for activevector = [a, b]
for passivevector = [c, d]
casestudy = [casetype, ' - active (', num2str(activevector), ...
') passive (', num2str(passivevector), ')'];
mkdir([name,casestudy]);
end
end
Neither [a, b], [c, d] not activevector or passivevector need to be transposed.
When a is a 1x2 vector, a' is a 2x1 vector. You cannot concatenate an 2x1 and 3x1 vector horizontally, because they have different numbers of rows.
The code fails due to a missing "name".
##### 2 CommentsShowHide 1 older comment
Gaetano Pavone on 12 Feb 2021
@Stephen Cobeldick please see my last comment

Bjorn Gustavsson on 12 Feb 2021
To fool-proof your concatenation, and to generate better directory-names you could do something like this:
a = [1 2];
b = [3 4 5];
c = [6 7];
d = [8 9 10 11];
casetype = 'trial';
for i_active = [a(:).' b(:).'] % (:) converts to column-array, then .' to row-array
for i_passive = [c(:).' d(:).'] % and then to long arrays
casestudy = sprintf('%s-active-%02d-passive-%02d',casetype,i_active,i_passive);
dirname = fullfile(name,casestudy);
mkdir(dirname);
end
end
The %02d is to make the directory-name more handy with leading zero-padding, that is just better. Don't insert white-spaces in file and directory-names, that is just asking for annoying troubles later on.
HTH
##### 2 CommentsShowHide 1 older comment
Bjorn Gustavsson on 12 Feb 2021
Not unfortunately, by design. Now with a modified question we're expected to modify our help. Is this the final question on this?
The answer here is not to store this information in the directory-names. This type of meta-data you should store in the data in your experiment parameters data-file. So I advice you to modify Stephen's suggestion below to something like this:
a = {[1,2],[3,4,5]};
p = {[6,7],[8,9,10,11]};
c = 'trial';
i_exp = 1;
for k1 = 1:numel(a)
for k2 = 1:numel(p)
dirname = fullfile(name,sprintf('Experiment-%02d',i_exp));
mkdir(dirname)
active = a{k1};
passive = p{k2}
save(fullfile(dirname,'experiment_parameters.mat'),'active','passive')
i_exp = i_exp + 1;
end
end
You might prefer slightly different directory names, but storing meta-data there is not the best way to do it and should definitely not be the only place where you store them.

Stephen Cobeldick on 12 Feb 2021
Edited: Stephen Cobeldick on 12 Feb 2021
a = {[1,2],[3,4,5]};
p = {[6,7],[8,9,10,11]};
c = 'trial';
for k1 = 1:numel(a)
for k2 = 1:numel(p)
s = sprintf('%s - active (%s) passive (%s)',c,...
join(string(a{k1})),...
join(string(p{k2})))
end
end
s = 'trial - active (1 2) passive (6 7)'
s = 'trial - active (1 2) passive (8 9 10 11)'
s = 'trial - active (3 4 5) passive (6 7)'
s = 'trial - active (3 4 5) passive (8 9 10 11)'

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by