How to Concatenate 5 differently named variables in one variable using foor loop?

조회 수: 5 (최근 30일)
Raza Ali
Raza Ali 2022년 1월 12일
편집: Stephen23 2022년 1월 12일
A_1=1;
A_2=2;
A_3=3;
A_4=4;
A_5=5;
for i=1:5
% now here i want to concatinate A_1 to A_5
end
Combine= [1 2 3 4 5] % this is required
  댓글 수: 4
Raza Ali
Raza Ali 2022년 1월 12일
Thank you for the comments. Actually, I have n variables and every variable has a different number of elements (a row matrix). So, I need to combine all the elements of all n variables.
Stephen23
Stephen23 2022년 1월 12일
편집: Stephen23 2022년 1월 12일
"Actually, I have n variables and every variable has a different number of elements (a row matrix). So, I need to combine all the elements of all n variables."
That is very easy if you have well-designed data, e.g. multiple vectors in one cell array C, using any of these:
V = [C{:}]
V = cat(n,C{:})
V = horzcat(C{:})
V = vertcat(C{:})
Trying to do that with badly-designed data (e.g. lots of variables with numbered names) will be much more complex and inefficient than that simple code. Better data design -> much better code.

댓글을 달려면 로그인하십시오.

답변 (1개)

VIGNESH B S
VIGNESH B S 2022년 1월 12일
a1 = 5;
a2 = 10;
a3 = 15;
combined_row_wise = [a1,a2,a3]
combined_row_wise = 1×3
5 10 15
combined_column_wise = [a1;a2;a3]
combined_column_wise = 3×1
5 10 15

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by