필터 지우기
필터 지우기

Simple Indexing problem when creating a matrix

조회 수: 1 (최근 30일)
Davide Cannavacciuolo
Davide Cannavacciuolo 2023년 11월 28일
편집: Matt J 2023년 11월 28일
I have this vector
A=[0 60 120 180 240 300 360];
And I have a variable n. Lets say n=24. I neet a matrix with the first row A and the other rows add to each element 360, n times so n rows.
A=[0 60 120 180 240 300 360;
360 420 ...............;
720 ...................];
How can I do it? I have tried with a for loop but had no success

채택된 답변

Matt J
Matt J 2023년 11월 28일
n=5;
A=[0 60 120 180 240 300 360];
A=A+360*(0:n-1)'
A = 5×7
0 60 120 180 240 300 360 360 420 480 540 600 660 720 720 780 840 900 960 1020 1080 1080 1140 1200 1260 1320 1380 1440 1440 1500 1560 1620 1680 1740 1800

추가 답변 (1개)

Drew
Drew 2023년 11월 28일
This is an easy question for the MATLAB AI Chat Playground https://www.mathworks.com/matlabcentral/playground/new (see announcement at https://blogs.mathworks.com/community/2023/11/07/the-matlab-ai-chat-playground-has-launched/). I asked your question and got a working response.
Code in runnable form:
A = [0 60 120 180 240 300 360];
n = 24;
B = repmat(A, n, 1) + (0:n-1)'*360;
B
B = 24×7
0 60 120 180 240 300 360 360 420 480 540 600 660 720 720 780 840 900 960 1020 1080 1080 1140 1200 1260 1320 1380 1440 1440 1500 1560 1620 1680 1740 1800 1800 1860 1920 1980 2040 2100 2160 2160 2220 2280 2340 2400 2460 2520 2520 2580 2640 2700 2760 2820 2880 2880 2940 3000 3060 3120 3180 3240 3240 3300 3360 3420 3480 3540 3600
Image form of the question and answer. Note that I added "Write MATLAB code to do the following" at the start of your question, telling the AI to write the code.
  댓글 수: 1
Matt J
Matt J 2023년 11월 28일
편집: Matt J 2023년 11월 28일
It is a strange answer. The AI is clearly aware of post-R2016b implicit expansion, and uses it in its answer, but does not realize that implicit expansion renders repmat unnecessary (and inefficient) here.

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

카테고리

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

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by