Split a Number Sequence into n equal parts and then replacing the values in the matrix.

15 Ansichten (letzte 30 Tage)
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 :
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,
Please help with the MATLAB Code?

Akzeptierte Antwort

Matt J
Matt J am 23 Mai 2021
Bearbeitet: Matt J am 23 Mai 2021
result = discretize(A, linspace(0, max(A(:)), 4) )-1
  5 Kommentare
Matt J
Matt J am 24 Mai 2021
Bearbeitet: Matt J am 24 Mai 2021
I odon't know what your adaptation looks like. Here's what I get,
Mat = [25 6 19 13 7 5; 7 8 10 12 14 17 ; 19 21 16 19 17 16 ; 19 19 17 15 13 11 ; 25 22 15 10 5 3 ; 25 23 20 19 17 1];
result = discretize(Mat, 1:5:26)-1
result = 6×6
4 1 3 2 1 0 1 1 1 2 2 3 3 4 3 3 3 3 3 3 3 2 2 2 4 4 2 1 0 0 4 4 3 3 3 0
Jonas
Jonas am 24 Mai 2021
@Matt J i think that was meant to be a comment for another answer below, there is an exact copy of the coment

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Jonas
Jonas am 23 Mai 2021
Bearbeitet: Jonas am 24 Mai 2021
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
  2 Kommentare
Mark Wood
Mark Wood am 23 Mai 2021
Bearbeitet: Mark Wood am 24 Mai 2021
This fails,
for example taking the matrix eg.
Mat = [25 6 19 13 7 5; 7 8 10 12 14 17 ; 19 21 16 19 17 16 ; 19 19 17 15 13 11 ; 25 22 15 10 5 3 ; 25 23 20 19 17 1];
Here nrOfIntervals is 5,
0 - 1:5
1 - 6:10
2 - 11:15
3 - 16:20
4 - 21-25
But here the element 5 in the Mat comes in 1 but it should be under 0.
So what I want to say that, here as number of equal parts was 5, so here the interval starts with 1 not 0.
Could you please help in modifying the code!
Jonas
Jonas am 24 Mai 2021
Bearbeitet: Jonas am 24 Mai 2021
my bad, this should work better:
nrOfIntervals=3;
A=A/max(A(:));
A=A*nrOfIntervals;
A=ceil(A);
A(A>0)=A(A>0)-1;

Melden Sie sich an, um zu kommentieren.


Girijashankar Sahoo
Girijashankar Sahoo am 23 Mai 2021
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

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by