according to the following code and output, i want to get the every 'm' array after the for loop one by one to apply to the rest of the code. when i write m(2) to get the 2nd array, it gives the last arrays' 2nd number as 3. how can i get the every array one by one
m=[1 2 3 4 5 6]
for k=1:5
m=circshift(m,1)
end
command window is as follows
m =
1 2 3 4 5 6
m =
6 1 2 3 4 5
m =
5 6 1 2 3 4
m =
4 5 6 1 2 3
m =
3 4 5 6 1 2
m =
2 3 4 5 6 1
>> m =
1 2 3 4 5 6
m =
6 1 2 3 4 5
m =
5 6 1 2 3 4
m =
4 5 6 1 2 3
m =
3 4 5 6 1 2
m =
2 3 4 5 6 1

 채택된 답변

Image Analyst
Image Analyst 2021년 8월 14일

0 개 추천

Try this to get a 2-D array with each version of m on one row of the 2-D matrix:
m=[1 2 3 4 5 6]
[rows, columns] = size(m);
all_m = zeros(columns, columns);
for k=1:columns
all_m(k, :) = circshift(m,k)
end
all_m =
6 1 2 3 4 5
5 6 1 2 3 4
4 5 6 1 2 3
3 4 5 6 1 2
2 3 4 5 6 1
1 2 3 4 5 6

댓글 수: 4

Kerem Yüksel
Kerem Yüksel 2021년 8월 14일
no need for all_m = zeros(columns, columns); but it works
thank you
Not really, but if you don't you'll get squiggly orange lines telling you to do it. It speeds things up if you preallocate memory in advance, though it won't be noticeable for such a tiny matrix.
By the way, you can adjust the k in the circshift function to get the rows in different order if you want. Like to put the 1 in the top left corner:
m=[1 2 3 4 5 6]
[rows, columns] = size(m);
all_m = zeros(columns, columns);
for k=1:columns
all_m(k, :) = circshift(m,k+5);
end
all_m % Report to command window.
Kerem Yüksel
Kerem Yüksel 2021년 8월 19일
how can i get these every array values out of for loop
Once the loop ends, you have all the array values in the variable called all_m. If you want the results from just one particular run, like #2, then do
valuesFromRun2 = all_m(2, :)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2021년 8월 14일

댓글:

2021년 8월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by