필터 지우기
필터 지우기

Find and delete a double value in matrix

조회 수: 1 (최근 30일)
Rihanna Waterson
Rihanna Waterson 2017년 2월 24일
댓글: Jan 2017년 2월 27일
I have several matrices with Double values. I want to delete(make null) n numbers. For example n=300 and I want to delete 300 values randomly. Here is a part of my code. I'm not sure if it's correct and how to continue it.
totalsize=size(matrixname,1)*size(matrixname,2);
n=totalsize*PER; %number of elements which values should be deleted
r = randi([1 totalsize],1,pen); %find n random elements to be deleted

답변 (2개)

James Tursa
James Tursa 2017년 2월 24일
Use randperm, not randi. The randperm function will ensure that you do not get duplicate indexes to delete, whereas using randi might produce duplicate indexes. Also, I am assuming PER is a percentage number, so you should use round or floor or ceil on the result of totalsize*PER to ensure it is an integer for downstream processing.
  댓글 수: 1
Rihanna Waterson
Rihanna Waterson 2017년 2월 24일
편집: Rihanna Waterson 2017년 2월 24일
Thank you. And how to find and delete value of each nth element in a matrix? I know I cannot use find() because of decimal numbers.

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


Jan
Jan 2017년 2월 24일
편집: Jan 2017년 2월 24일
totalsize = numel(matrixname);
n = round(totalsize*PER);
index = randperm(totalsize, n); % n eandom distinct values from 1:totalsize
matrixname(index) = 0;
To delete each n.th element:
matrixname(1:n:totalsize) = 0;
  댓글 수: 2
Rihanna Waterson
Rihanna Waterson 2017년 2월 24일
Thanks. But they shouldn't be zero...because it will affect image reconstruction. I should delete them so they won't be used in image reconstruction. I've written this part but it seems the last line is not correct.
PER=0.0001;
totalsize=size(y_blk,1)*size(y_blk,2);
pen=floor(totalsize*PER);
r = randperm (totalsize,pen);
for l=0:pen
rem=rem(r,size(y_blk,1)); %row
div=floor(r/size(y_blk,1))+1; %column
y_blk(rem,div)=[];
end
Jan
Jan 2017년 2월 27일
@Rihanna: The term "delete(make null)" was not clear to me.
Please explain "it seems the last line is not correct" with details. I cannot run your code to check, what's going on. Note that deleting elements in a matrix might conflict with the definition of a matrix, that all columns have the same number of rows and vice versa.

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by