Could anyone help me how to get the result as desired in the following code.

2 次查看(过去 30 天)
A=1:20;
while ~isempty(A)
B=reshape(A,[],2);
B(:,end)=B(end:-1:1,end);
for k=1:size(B,1)
C=B(k,:)
A=[];
end
end
The code executes and gives the following result
C = 1 20
C = 2 19
C = 3 18
C = 4 17
C = 5 16
C = 6 15
C = 7 14
C = 8 13
C = 9 12
C = 10 11
But I want to have the result in the following manner
C = 1 20 4 17 7 14 10 11
C = 2 19 5 16 8 13
C = 3 18 6 15 9 12
Could anyone please help me on this.
  3 个评论
jaah navi
jaah navi 2021-11-3
With respect to the above code I am having 20 elements. The reason I am saving it to C every time is that, I need to compare the performance metrices based on the elements
C = 1 20 4 17 7 14 10 11
C = 2 19 5 16 8 13
C = 3 18 6 15 9 12
that have been stored in C.
Constantino Carlos Reyes-Aldasoro
If you need those specific values to run something else, then there is not much point in coding the order. Just store as cells that you can use
C={[1 20 4 17 7 14 10 11],[2 19 5 16 8 13 ],[3 18 6 15 9 12]}
And then use them in your code like
for k=1:3
C{k}
% ...
%...
end

请先登录,再进行评论。

采纳的回答

Mathieu NOE
Mathieu NOE 2021-11-3
hello
after so guess work, I finally change the code to this
clc
A=1:20;
B=reshape(A,[],2);
B(:,end)=B(end:-1:1,end);
for ci = 1:3
C = [];
ind1 = ci:3:size(B,1);
for k=1:length(ind1)
ind2 = ci+(k-1)*3;
C = [C B(ind2,:)];
end
disp(C)
end
and this is the result , as expected :
1 20 4 17 7 14 10 11
2 19 5 16 8 13
3 18 6 15 9 12
maybe those 3 lines should be stored in 3 cells ?

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by