필터 지우기
필터 지우기

Elementwise shift of two matrices

조회 수: 1 (최근 30일)
Siddgr
Siddgr 2022년 9월 15일
댓글: Walter Roberson 2022년 9월 15일
I have two 1xn matrices each of which with different index for their minima.
For instance the minimum of matrix A is the 3rd index:
A = [9 8 7 8 9];
[i idx_A] = min(A); idx_A
idx_A = 3
whereas the minimum of matrix B is the 4th index:
B = [8 8 7 6 9];
[i idx_B] = min(B); idx_B
idx_B = 4
How can I shift these two such that they both have their minima on the same index?
One way that comes to mind is cutting matrices from left and right. At the end of the day, I wan to get a matrix that looks like below:
A = [9 8 7 8];
[i idx_A] = min(A); idx_A
idx_A = 3
B = [8 7 6 9];
[i idx_B] = min(B); idx_B
idx_B = 3
How can this be done in a way that work for all matrices with the same size?

채택된 답변

Bruno Luong
Bruno Luong 2022년 9월 15일
편집: Bruno Luong 2022년 9월 15일
truncated matrices
[~, idx_A] = min(A);
[~, idx_B] = min(B);
di=idx_A-idx_B;
Ac=A(max(1,1+di):min(end,end+di))
Bc=B(max(1-di,1):min(end-di,end))

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 9월 15일
B = [8 8 7 6 9];
[i idx_B] = min(B);
circshift(B, 1-idx_B)
ans = 1×5
6 9 8 8 7
  댓글 수: 1
Walter Roberson
Walter Roberson 2022년 9월 15일
You could examine the relative values of the indices and the length of the vectors to figure out which one would involve "less" work to move.. but it would probably involve more coding than the situation is worth.
Oh... I just had a thought: you might have a situation in which the minima for one of the vectors is beyond the length of the other vector. So if you have a hard rule about which one to shift, you could end up requiring that one of them be padded.
A = [9 8 7 8];
[~, idx_A] = min(A);
B = [8 8 7 6 9];
[~, idx_B] = min(B);
B = circshift(B, idx_A - idx_B);
A, B
A = 1×4
9 8 7 8
B = 1×5
8 7 6 9 8

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

카테고리

Help CenterFile Exchange에서 Composite Interfaces에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by