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

2 views (last 30 days)
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 Comments
jaah navi
jaah navi on 3 Nov 2021
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

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 3 Nov 2021
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 ?

More Answers (0)

Community Treasure Hunt

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

Start Hunting!