How to replace zero with the values present in array

Hello Everyone, I hope you are doing well. I have an array named as Dataset. I have replace 0 in for those values which has histogram count less than 50 percent of maximum value.
Now in my new array Dataset Has some Values which are equal to 0; I want to replace 0 with the Values already present in the array.
For example the array consists of value
34.9609375000000
37.9882812500000
34.9609375000000
These Values should be in placed of 0's
How can i do it in Matlab
Batchdata=Dataset;
fig=figure; set(fig,'visible','off');
h=histogram(Batchdata,10000);
sumofbins=max(h.Values);
size_MP=round(50/100*sumofbins);
ValueofHistogram= h.Values;
Bindata=h.Data;
Binedges=h.BinEdges;
Binedges(end) = Inf;
deleted_data_idx = false(size(Bindata));
for i=1: length(ValueofHistogram)
if ValueofHistogram(i)<size_MP;
deleted_data_idx(Bindata >= Binedges(i) & Bindata < Binedges(i+1)) = true;
end
end
close(fig);
Dataset(deleted_data_idx,:)=0;

댓글 수: 5

For example the array consists of value ... These Values should be in placed of 0's
Your example isn't actually an example. It doesn't show us a hypothetical starting Dataset nor the final transformed one.
@Matt J It is actually example. 0 should be replaced by the the values which are present in the dataset . How can i do that?
Matt J
Matt J 2022년 8월 26일
편집: Matt J 2022년 8월 26일
So, if
array=[34.9609375000000 37.9882812500000 34.9609375000000];
DataSet=[100 800 0 900];
then the result should be
newDataSet=[100 800 34.9609375000000 37.9882812500000 34.9609375000000 900];
I replace each zero with the entirety of array?
@Matt J No, each zero Replace with single value
For examples
DataSet=[100 800 0 900 0 100 0 0];
Then New Dataset is
DataSet=[100 800 800 900 900 100 800 900];
@Matt J There is second method. We will delete, the 0's values and repeat the array from the start to complete. same size as original
For examples I have 8 entries in the dataset
DataSet=[100 800 0 900 0 100 0 0];
New dataset after deleted 0; we have four entries
DataSet=[100 800 900 100 ];
and at last repeat the matrix to complete 8 entries
DataSet=[100 800 900 100 100 800 900 100];

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

답변 (1개)

dpb
dpb 2022년 8월 26일
이동: Matt J 2022년 8월 26일
array=[34.9609375000000
37.9882812500000
34.9609375000000];
Dataset(deleted_data_idx,:)=repmat(array,1,size(Dataset,2));
presuming
numel(array) == sum(deleted_data_idx)

댓글 수: 10

@dpb The array values array=[34.9609375000000
37.9882812500000
34.9609375000000]; are already in the dataset.
@dpb There is second method. We will delete, the 0's values and repeat the array from the start to complete. same size as original
For examples I have 8 entries in the dataset
DataSet=[100 800 0 900 0 100 0 0];
New dataset after deleted 0; we have four entries
DataSet=[100 800 900 100 ];
and at last repeat the matrix to complete 8 entries
DataSet=[100 800 900 100 100 800 900 100];
DataSet=[100 800 0 900 0 100 0 0];
N1=numel(DataSet);
DataSet=DataSet(find(DataSet));
N2=numel(DataSet);
>> DataSet=[DataSet DataSet(1:N1-N2)]
DataSet =
100 800 900 100 100 800 900 100
>>
Above works if N2 >= N1/2
If N2 < N1/2, will have to reproduce remainder DataSet ceil(N1/N2) times and then keep N1 terms. Will leave exact code as "exercise for student"...
I have no klew what the first was intended to be, then...don't understand the specific problem trying to solve; too convoluted an explanation to try to delve into.
@dpb How to make this code generalized?
Because the first apporach is not working. So i changed the apporach, Thats why i asked
Which code is that? The last algorithm is general with the caveat noted that you've got to duplicate more than once to recreate the original length if you remove more than half the original elements.
@dpb How can ceil. Can we do it using repmat command?
Above works if N2 >= N1/2
If N2 < N1/2, will have to reproduce remainder DataSet ceil(N1/N2) times and then keep N1 terms. Will leave exact code as "exercise for student"...
dpb
dpb 2022년 8월 27일
편집: dpb 2022년 8월 27일
"...do it using repmat command?"
How many times (but only as many times as needed)????
@dpb repeat to complete 1000 samples
You're missing the point -- how to calculate that number of repetitions is where the hint and ceil() come into play.

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2022년 8월 26일

댓글:

dpb
2022년 8월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by