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 个评论
Constantino Carlos Reyes-Aldasoro
2021-11-3
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
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 Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!