Could anyone help me how to get the result as desired in the following code.
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Constantino Carlos Reyes-Aldasoro
le 3 Nov 2021
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
Réponse acceptée
Mathieu NOE
le 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 ?
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!