Creating a string with Permutation

조회 수: 6 (최근 30일)
Chris Dan
Chris Dan 2022년 7월 15일
답변: Voss 2022년 7월 15일
Hi,
I have a short question, I am creating a string with a loop but I cannot do the permutation/combination correctly
here is my code
Axes_name = {'sint0.2';'sint0.5';'sint0.7'};
fold_2 = {'BCA';'BPCA';'DLA';'DLCA'};
C_ext = nan*zeros(length(Axes_name),length(fold_2));
for i=1:length(Axes_name)
for j=1:length(fold_2)
leg{sub2ind(size(C_ext),i,j)} = [Axes_name{i} ' ' fold_2{j}];
end
end
I am trying to make combinations like this:
sint0.2 BCA
sint0.2 BPCA
sint0.2 DLA
sint0.2 DLCA
sint0.5 BCA
sint0.5 BCPA
sint0.5 DLA
sint0.5 DLCA
...
and so on, but i am not getting it correctly.Does any know what changes i should do

채택된 답변

Adam Danz
Adam Danz 2022년 7월 15일
편집: Adam Danz 2022년 7월 15일
I believe this is what you're working toward.
Axes_name = {'sint0.2';'sint0.5';'sint0.7'};
fold_2 = {'BCA';'BPCA';'DLA';'DLCA'};
str = compose('%s %s', string(Axes_name), string(fold_2)')
str = 3×4 cell array
{'sint0.2 BCA'} {'sint0.2 BPCA'} {'sint0.2 DLA'} {'sint0.2 DLCA'} {'sint0.5 BCA'} {'sint0.5 BPCA'} {'sint0.5 DLA'} {'sint0.5 DLCA'} {'sint0.7 BCA'} {'sint0.7 BPCA'} {'sint0.7 DLA'} {'sint0.7 DLCA'}
If you want a column vector,
str(:)
ans = 12×1 cell array
{'sint0.2 BCA' } {'sint0.5 BCA' } {'sint0.7 BCA' } {'sint0.2 BPCA'} {'sint0.5 BPCA'} {'sint0.7 BPCA'} {'sint0.2 DLA' } {'sint0.5 DLA' } {'sint0.7 DLA' } {'sint0.2 DLCA'} {'sint0.5 DLCA'} {'sint0.7 DLCA'}

추가 답변 (2개)

Paul
Paul 2022년 7월 15일
Hi hazmah,
Is this what you're looking for?
Axes_name = string({'sint0.2';'sint0.5';'sint0.7'})
Axes_name = 3×1 string array
"sint0.2" "sint0.5" "sint0.7"
fold_2 = string({'BCA';'BPCA';'DLA';'DLCA'})
fold_2 = 4×1 string array
"BCA" "BPCA" "DLA" "DLCA"
result = reshape(Axes_name.' + " " + fold_2,[],1)
result = 12×1 string array
"sint0.2 BCA" "sint0.2 BPCA" "sint0.2 DLA" "sint0.2 DLCA" "sint0.5 BCA" "sint0.5 BPCA" "sint0.5 DLA" "sint0.5 DLCA" "sint0.7 BCA" "sint0.7 BPCA" "sint0.7 DLA" "sint0.7 DLCA"

Voss
Voss 2022년 7월 15일
Axes_name = {'sint0.2';'sint0.5';'sint0.7'};
fold_2 = {'BCA';'BPCA';'DLA';'DLCA'};
You can change the size of C_ext to be length(fold_2)-by-length(Axes_name), since you want to cycle through the fold_2 values first in leg. (Swap j and i where they're used in sub2ind as well.)
C_ext = nan*zeros(length(fold_2),length(Axes_name));
for i=1:length(Axes_name)
for j=1:length(fold_2)
leg{sub2ind(size(C_ext),j,i)} = [Axes_name{i} ' ' fold_2{j}];
end
end
leg = leg.'
leg = 12×1 cell array
{'sint0.2 BCA' } {'sint0.2 BPCA'} {'sint0.2 DLA' } {'sint0.2 DLCA'} {'sint0.5 BCA' } {'sint0.5 BPCA'} {'sint0.5 DLA' } {'sint0.5 DLCA'} {'sint0.7 BCA' } {'sint0.7 BPCA'} {'sint0.7 DLA' } {'sint0.7 DLCA'}
However, you can create matrices of indices i and j using meshgrid or ndgrid, and construct leg at once using strcat:
[i,j] = meshgrid(1:numel(Axes_name),1:numel(fold_2));
leg = reshape(strcat(Axes_name(i),{' '},fold_2(j)),[],1)
leg = 12×1 cell array
{'sint0.2 BCA' } {'sint0.2 BPCA'} {'sint0.2 DLA' } {'sint0.2 DLCA'} {'sint0.5 BCA' } {'sint0.5 BPCA'} {'sint0.5 DLA' } {'sint0.5 DLCA'} {'sint0.7 BCA' } {'sint0.7 BPCA'} {'sint0.7 DLA' } {'sint0.7 DLCA'}

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by