필터 지우기
필터 지우기

How to find index of non-alternate matrix elements?

조회 수: 1 (최근 30일)
Megha
Megha 2018년 12월 25일
댓글: Megha 2018년 12월 26일
i have two matrices say,
clear; close all; clc;
A = [1 3 5 7 9 11 13]; % nx1 matrix
B = [2 4 6 8 10 12 14]; % mx1 matrix
C = [A,B];
C = C';
D = sort(C);
This gives me matrix 'D' with elements from both A and B alternately.
However, if my matrix changes a bit in between like
clear; close all; clc;
A = [1 3 7 9 11 13]; % nx1 matrix
B = [2 4 6 8 10 12 14]; % mx1 matrix
C = [A,B];
C = C';
D = sort(C);
Then my matrix D with not have continuous alternate values from both matrix.
How can i find the index of this discontinuity, each time when the value repeats?
The final matrix D should have continuously increasing values.
  댓글 수: 3
Walter Roberson
Walter Roberson 2018년 12월 25일
Looks to me as if you should be considering
D = reshape([A;B], 1, []);
Stephen23
Stephen23 2018년 12월 25일
@Walter Roberson: except in the second example A and B have different numbers of elements.

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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2018년 12월 25일
A = [1 3 7 9 11 13];
B = [2 4 6 8 10 12 14];
C = {A(:);B(:)};
M = [cell2mat(C),repelem([1;2],cellfun(@(x)numel(x),C))];
S = sortrows(M,1);
idx_out = find([false;diff(S(:,2))==0]);
  댓글 수: 1
Megha
Megha 2018년 12월 26일
thanks! Your suggetsion really worked for me.
Thnak you for putting effeorts!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by