how to do for loop 2nd level?

8 vues (au cours des 30 derniers jours)
Arif
Arif le 11 Mar 2024
Commenté : Arif le 16 Mar 2024
hi guys, i want to do :
XY :
AA
AB
AC
AD
..
QQ
but this error happened.
  1 commentaire
Arif
Arif le 11 Mar 2024
Here is my code
clc
clear
beam = {'A';'B';'C';'D';'E';'F';'G';'H';'I';'J';'K';'L';'M';'N';'O';'P';'Q'}
column = {'A';'B';'C';'D';'E';'F';'G';'H';'I';'J';'K';'L';'M';'N';'O';'P';'Q'}
for i = 1 : length(beam)
x(i) = string(beam(i))
for j = 1 : length(column)
y(j) = string(column(j))
xy(i) = append(x(i),y(j))
end

Connectez-vous pour commenter.

Réponse acceptée

Rik
Rik le 11 Mar 2024
Modifié(e) : Rik le 11 Mar 2024
Your code can be optimized, but here are the minimal changes.
beam = {'A';'B';'C';'D';'E';'F';'G';'H';'I';'J';'K';'L';'M';'N';'O';'P';'Q'};
column = {'A';'B';'C';'D';'E';'F';'G';'H';'I';'J';'K';'L';'M';'N';'O';'P';'Q'};
xy=repmat("",numel(beam)*numel(column),1);
k=0;
for i = 1 : numel(beam)
x = string(beam(i));
for j = 1 : numel(column)
y = string(column(j));
k=k+1;
xy(k) = x+y;
end
end
xy
xy = 289×1 string array
"AA" "AB" "AC" "AD" "AE" "AF" "AG" "AH" "AI" "AJ" "AK" "AL" "AM" "AN" "AO" "AP" "AQ" "BA" "BB" "BC" "BD" "BE" "BF" "BG" "BH" "BI" "BJ" "BK" "BL" "BM"
Now for the more compact version (using implicit expansion and transposing with .'):
beam = {'A';'B';'C';'D';'E';'F';'G';'H';'I';'J';'K';'L';'M';'N';'O';'P';'Q'};
column = {'A';'B';'C';'D';'E';'F';'G';'H';'I';'J';'K';'L';'M';'N';'O';'P';'Q'};
xy = string(beam).'+string(column);
xy = xy(:);
xy
xy = 289×1 string array
"AA" "AB" "AC" "AD" "AE" "AF" "AG" "AH" "AI" "AJ" "AK" "AL" "AM" "AN" "AO" "AP" "AQ" "BA" "BB" "BC" "BD" "BE" "BF" "BG" "BH" "BI" "BJ" "BK" "BL" "BM"
Or perhaps even this:
beam = string(num2cell('A':'Q'));
column = string(num2cell('A':'Q'));
xy = reshape(beam+column.',[],1);
xy
xy = 289×1 string array
"AA" "AB" "AC" "AD" "AE" "AF" "AG" "AH" "AI" "AJ" "AK" "AL" "AM" "AN" "AO" "AP" "AQ" "BA" "BB" "BC" "BD" "BE" "BF" "BG" "BH" "BI" "BJ" "BK" "BL" "BM"
  1 commentaire
Arif
Arif le 16 Mar 2024
thanks

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by