Dynamically arrange the matrix in each for loop

I have two matrix a = [3 5 6 7] and b = [2 4 6 1; 5 7 8 9]. I need to dynamic arrange the matrix b when the position of a is changing.
For example,
First loop, a = [3 5 6 7]
b = [2 4 6 1
5 7 8 9]
But at the second loop, the position for b is changed a = [7 3 5 6]
b = [1 2 4 6
9 5 7 8]
and so on for the subsequent loop. Is this possible to happen? Thank you.

 채택된 답변

Eric Pahlke
Eric Pahlke 2011년 12월 15일

0 개 추천

Definitely possible.
a = [3 5 6 7];
b = [2 4 6 1; 5 7 8 9];
for ii = 1:4
% do_stuff(a,b);
disp('iteration:')
disp(ii);
a
b
a = [a(end) a(1:end-1)];
b = [b(:,end) b(:,1:end-1)];
end

댓글 수: 4

Chin
Chin 2011년 12월 15일
How about each time the position for the b is random? Which means the
iteration 1 b = [3 5 6 7]
iteration 2 b = [5 3 7 6]
iteration 3 b = [7 3 5 6]
iteration 3 b = [6 5 7 3]
and so on. Thank you.
c = cell(1,4)
for j1 = 1:4
c{j1} =circshift([a;b],[0 j1]);
end
Chin
Chin 2011년 12월 15일
The problem is when a = [6 5 3 7]
i will get the result b = [6 4 2 1; 8 7 5 9]. Is that possible need to use indexing?
x = a(randperm(4));
[loc,loc] = ismember(x,a);
b1 = b(:,loc)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2011년 12월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by