how to do for loop 2nd level?

3 ビュー (過去 30 日間)
Arif
Arif 2024 年 3 月 11 日
コメント済み: Arif 2024 年 3 月 16 日
hi guys, i want to do :
XY :
AA
AB
AC
AD
..
QQ
but this error happened.
  1 件のコメント
Arif
Arif 2024 年 3 月 11 日
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

サインインしてコメントする。

採用された回答

Rik
Rik 2024 年 3 月 11 日
編集済み: Rik 2024 年 3 月 11 日
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 件のコメント
Arif
Arif 2024 年 3 月 16 日
thanks

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by