Split a Number Sequence into n equal parts and then replacing the values in the matrix.
조회 수: 22 (최근 30일)
이전 댓글 표시
How to split a number sequence into n equal parts and then and then replace the value of matrix with it.
For Example:
A 5x5 Matrix(A) is given to us :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/627518/image.png)
Now we will find the maximum element in this matrix using : M = max(A, [], 'all')
Now we want to quantize this max number ie. 29 into n equal parts. Example : Now starting from 0 to 29 ([0:29]) I want to split this sequence to 3 equal parts.
Such that my output now looks like:
0 - 0:9
1 - 10:19
2 - 20:29
Now I want to replace the number that are lying between 0 to 9 with 0, 10 to 19 with 1 and 20 to 29 with 2 from the given matrix.
Such that my final matrix becomes,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/627523/image.png)
Please help with the MATLAB Code?
댓글 수: 0
채택된 답변
Matt J
2021년 5월 23일
편집: Matt J
2021년 5월 23일
result = discretize(A, linspace(0, max(A(:)), 4) )-1
추가 답변 (2개)
Jonas
2021년 5월 23일
편집: Jonas
2021년 5월 24일
i suggest normalizing the matrix by dividing all elements through the biggest value you already have and multiply it afterwards with the number of intervals you want (3 in the example). then you can apply the floor() function. the values equal to the maximum value have to be decreased by one at the end (e.g. 29 will become 3 and then has the be decreased to 2, all other wntries are already correct)
nrOfIntervals=3;
A=A/max(A(:));
A=A*nrOfIntervals;
A=floor(A);
A(A==nrOfIntervals)=nrOfIntervals-1;
edit: a correct version can be found in the comments of this answer
Girijashankar Sahoo
2021년 5월 23일
N= 25 % length of sequence
n=sqrt(N) % break the sequence in n element
X=randi(30,1,25)
A=reshape(X,[n,n])
M=max(A,[],'all')
split=M/3
for i=1:n
for j=1:n
Value=A(i,j)
if Value<(M/split)
A(i,j)=0;
elseif (M/split)<Value<(2*M/split)
A(i,j)=1;
else Value>(2*M/split)
A(i,j)=2;
end
end
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!