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

1 回表示 (過去 30 日間)
jaah navi
jaah navi 2021 年 11 月 3 日
コメント済み: jaah navi 2021 年 11 月 3 日
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
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
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 ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by