Left-hand side indexing issue

Dear all,
I have a matrix A:
A = [1,2,3,4,5,6,7,8; 1,2,3,4,5,6,7,8];
I would like to change the order of matrix columns. If I try for example:
B(:,[4 3 2 1 8 7 6 5]) = A
I get:
B =
4 3 2 1 8 7 6 5
4 3 2 1 8 7 6 5
which is correct.
Now I try:
B(:,[3 8 2 1 6 7 5 4])=A
and I get this weird result:
B =
4 3 1 8 7 5 6 2
4 3 1 8 7 5 6 2
Would you have an explanation? Thank you very much!
Tobard

 채택된 답변

Titus Edelhofer
Titus Edelhofer 2011년 11월 28일

1 개 추천

Hi,
why is it weird? You have for A:
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
Now you assign to the third, eighth, ... column of B the values of A, so the third column of B takes the 1, the eighth column takes the 2 and so on. Or: what did you expect?
Titus

추가 답변 (2개)

Tobard
Tobard 2011년 11월 28일

0 개 추천

Oh I understand what is my problem. I was expecting B to be:
3 8 2 1 6 7 5 4
3 8 2 1 6 7 5 4
So I expected the written indexes to be the column indexes of A in the desired order as andrei bobrov asked. The example I chose unfortunately didn't allow me to understand this mistake.
Thank you all!
Andrei Bobrov
Andrei Bobrov 2011년 11월 28일

0 개 추천

here all right
You want is it:
B = A(:,[3 8 2 1 6 7 5 4])

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by