for loop with vectors having different sizes

5 views (last 30 days)
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?
  7 Comments
Gaetano Pavone
Gaetano Pavone on 11 Feb 2021
I would like that my code creates a list of folders named for example: casetype - active (a) passive (c), and all possible combinations
Stephen23
Stephen23 on 12 Feb 2021
Edited: Stephen23 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)'

Sign in to comment.

Answers (3)

Jan
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 Comments
Gaetano Pavone
Gaetano Pavone on 12 Feb 2021
Unfortunately, your code executes the loop over each elements of the vectors. I need to traspose for iterating over the values of a single vector.
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..

Sign in to comment.


Bjorn Gustavsson
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 Comments
Gaetano Pavone
Gaetano Pavone on 12 Feb 2021
Unfortunately, your code executes the loop over each elements of the vectors. Please, see my edited question
Bjorn Gustavsson
Bjorn Gustavsson on 12 Feb 2021
Edited: 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.

Sign in to comment.


Stephen23
Stephen23 on 12 Feb 2021
Edited: Stephen23 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)'

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!