for loop with vectors having different sizes
이전 댓글 표시
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
2021년 2월 11일
Gaetano Pavone
2021년 2월 11일
Stephen23
2021년 2월 11일
What is the goal of the (complex conjugate) tranpose? Why do you need it?
Gaetano Pavone
2021년 2월 11일
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)
답변 (3개)
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".
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
2021년 2월 12일
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.
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
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!