find how many times duplicates occur in a matrix across row

조회 수: 4 (최근 30일)
sparsh garg
sparsh garg 2021년 9월 21일
답변: Jan 2021년 9월 21일
Given a matrix of 2xn dimension,in the row dimension there are several numbers with duplicates.Usnig histcnt these value shave their count atmost 2.
Now I would like to ensure that I only consider those elements in the row if their count is 1,for any number i will skip them.
I tried doing unique(A(1,:)) on my matrix but it didn't give me the correct result
My current code for duplicate findinng results in index exceed error
function ids=find_indices(A,B)
ids=[];
for i=1:size(A,2)
for j=1:size(B,2)
if(A(1,i-1)==A(1,i))
ctr=ctr+1;
end
%ctr=sum(A(1,:)==A(1,i));
if (B(1,j)==A(1,i) && ctr==1)
ids=[ids,i];
else
continue
end
end
end
end
  댓글 수: 5
sparsh garg
sparsh garg 2021년 9월 21일
oh sorry about that,what I wanted to say was that each value in the A matrix has a count of atmost 2 which is supported by the code snippet that you posted.
Now ,based on this I only want to select the values in A which are occuring only once. However after doing unique I am only interested in knowing the indice corresponding to these values
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
Columns 23 through 44
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128.
For better visualization of what B matrix looks like ,enclosed is a plot.
Note that the figure was obtained after applying curve fitting.
sparsh garg
sparsh garg 2021년 9월 21일
Hey Jan if possible could you give me some tips on how to go about this also
https://in.mathworks.com/matlabcentral/answers/1457689-how-to-write-design-a-matrix-similar-to-the-one-outputted-by-contourf?s_tid=prof_contriblnk

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

답변 (1개)

Jan
Jan 2021년 9월 21일
As fas as I understand, you want to obtain the indices of the elements of A, which occur once only. Then:
index = ~isMultiple(A(1, :));
result = A(1, index); % Or maybe sorted:
sort(A(1, index)
function T = isMultiple(A)
[S, idx] = sort(A(:).');
m = [false, diff(S) == 0];
ini = strfind(m, [false, true]);
m(ini) = true; % Mark 1st occurence in addition
T(idx) = m;
end

카테고리

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