How to create a row from x(1) to x(n)?

조회 수: 6 (최근 30일)
Alex M
Alex M 2020년 11월 12일
답변: Image Analyst 2020년 11월 12일
Hello
I am quite new to coding and trying to create a row where each term in the row is x followed by the column number in brackets. So [x(1) x(2) x(3) ... x(n)]
Many thanks

채택된 답변

Cris LaPierre
Cris LaPierre 2020년 11월 12일
I suggest going through MATLAB Onramp. Chps 4 and 5 seems to be relevant to what you are asking.
You might also just be needing to transpose you column vector to turn it into a row vector. Use ' for that. This is covered in 4.2.6.
x=[1:5]'
x = 5×1
1 2 3 4 5
x1=x'
x1 = 1×5
1 2 3 4 5
  댓글 수: 3
Cris LaPierre
Cris LaPierre 2020년 11월 12일
Ok, that is pretty simple as well, but not covered in Onramp.
x1 = "X(" + string(1:5) + ")"
x1 = 1×5 string array
"X(1)" "X(2)" "X(3)" "X(4)" "X(5)"
Alex M
Alex M 2020년 11월 12일
Thanks!

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

추가 답변 (1개)

Image Analyst
Image Analyst 2020년 11월 12일
Try this:
n = 5;
for k = 1 : n
str(k) = string(sprintf('x(%d)', k));
end
str % Display in command window
You'll see:
str =
1×5 string array
"x(1)" "x(2)" "x(3)" "x(4)" "x(5)"

카테고리

Help CenterFile Exchange에서 String에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by