how to do for loop 2nd level?

1 visualización (últimos 30 días)
Arif
Arif el 11 de Mzo. de 2024
Comentada: Arif el 16 de Mzo. de 2024
hi guys, i want to do :
XY :
AA
AB
AC
AD
..
QQ
but this error happened.
  1 comentario
Arif
Arif el 11 de Mzo. de 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

Iniciar sesión para comentar.

Respuesta aceptada

Rik
Rik el 11 de Mzo. de 2024
Editada: Rik el 11 de Mzo. de 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"

Más respuestas (0)

Categorías

Más información sobre Symbolic Math Toolbox en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by