フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

How to use an index of a vector as a value in another matrix?

1 回表示 (過去 30 日間)
Syed Fahad Hassan
Syed Fahad Hassan 2017 年 8 月 3 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hi, I have a vector x=[1 1 2 2 3] and a matrix [1 3 ; 1 4 ; 2 3 ; 2 4]
The logic that I want to create says that the index of the latter of the similar values is extracted. Then, wherever the matrix has that value, that row becomes zero. For example, the indices of first two similar values in vector x are 1 and 2. I want those rows in the matrix that contains the value 2, should become zero. Same is the case for 3 and 4.
In the end, the matrix should only contain a single row i.e. [1 3]
  3 件のコメント
Matthew Eicholtz
Matthew Eicholtz 2017 年 8 月 3 日
I think when you say "becomes zero", you mean "is removed", correct?
Syed Fahad Hassan
Syed Fahad Hassan 2017 年 8 月 4 日
Yes Matthew, I want the row to be removed

回答 (3 件)

dpb
dpb 2017 年 8 月 3 日
>> x=[1 1 2 2 3] ;
>> m= [1 3 ; 1 4 ; 2 3 ; 2 4];
>> m(any(ismember(m,find(diff(x))),2),:)=[]
m =
1 3
>>
  1 件のコメント
Stephen23
Stephen23 2017 年 8 月 4 日
+1 nice use of indexing.

Alex Kerzner
Alex Kerzner 2017 年 8 月 3 日
I'm sure someone can come up with something more clever and efficient, but here's something hack-y that might give you some ideas for improvements. Assuming the matrix is called M :
repeats = [];
while ~isempty(x)
if numel(find(x == x(1))) >= 2 %If we have multiple of them
repeats(end+1) = max(find(x==x(1))); %add the largest index to the list
x(find(x==x(n))) = []; %remove all the duplicates
else
x(1) = []; %If no duplicates, just get rid of it
end
end
for r = repeats
[i,~] = find(M == r);
M(i,:) = [];
end
  1 件のコメント
Syed Fahad Hassan
Syed Fahad Hassan 2017 年 8 月 4 日
what is 'n' in your logic?

Matthew Eicholtz
Matthew Eicholtz 2017 年 8 月 3 日
How about this?
Assume you have the inputs:
x = [1,1,2,2,3];
y = [1,3; 1,4; 2,3; 2,4];
Then, you can find indices to remove by:
[~,ind,~] = unique(fliplr(x));
ind = length(x)-ind+1;
Then, remove the rows containing any of those indices:
y(any(ismember(y,ind),2),:) = [];
  1 件のコメント
Matthew Eicholtz
Matthew Eicholtz 2017 年 8 月 3 日
Note, my approach will remove indices of values that exist only once in x (e.g., the 3). It is unclear from the question whether this should be allowed or not.

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by