for loop with vectors having different sizes

조회 수: 2 (최근 30일)
Gaetano Pavone
Gaetano Pavone 2021년 2월 11일
편집: Bjorn Gustavsson 2021년 2월 12일
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
Gaetano Pavone
Gaetano Pavone 2021년 2월 11일
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 2021년 2월 12일
편집: Stephen23 2021년 2월 12일
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)'

댓글을 달려면 로그인하십시오.

답변 (3개)

Jan
Jan 2021년 2월 12일
편집: Jan 2021년 2월 12일
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
Gaetano Pavone
Gaetano Pavone 2021년 2월 12일
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..
Gaetano Pavone
Gaetano Pavone 2021년 2월 12일
@Stephen Cobeldick please see my last comment

댓글을 달려면 로그인하십시오.


Bjorn Gustavsson
Bjorn Gustavsson 2021년 2월 12일
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
Gaetano Pavone
Gaetano Pavone 2021년 2월 12일
Unfortunately, your code executes the loop over each elements of the vectors. Please, see my edited question
Bjorn Gustavsson
Bjorn Gustavsson 2021년 2월 12일
편집: Bjorn Gustavsson 2021년 2월 12일
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.

댓글을 달려면 로그인하십시오.


Stephen23
Stephen23 2021년 2월 12일
편집: Stephen23 2021년 2월 12일
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)'

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by