Re-write repeated values of a matrix using intermediate increased values
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Cesar Antonio Lopez Segura
il 5 Set 2018
Modificato: Cesar Antonio Lopez Segura
il 6 Set 2018
Hi all, I have some problems to rewrite a matrix with the values that I need. The following code do this:
- Step0 = a matrix is created
- Step1 = sort my matrix with ascend order
- Step2 = create a mask with zeros
- Step3 = here mask is rewrited with zeros just in repeated values positions
- Step4 = create a matrix with repeated values changed by zeros.
- Step5 = need to rewrite zero values with intermediate increased values (help me here !!)
%%step0: My matrix
A = [ 1 1 1 1 2 2 3 4 5 7 5;
3 3 6 5 4 1 5 7 8 9 1 ] ;
%%step1: sort my matrix ascend order
Asorted = sort(A,2);
%%step2: create a mask
Mask = zeros( size(Asorted,1),size(Asorted,2));
%%step3: Now Mask is rewrited with zeros in repeated values positons
for i = 1: size(Asorted,1)
[ C,ia,ic ] = unique( Asorted(i,:) );
Mask( i, ia ) = 1;
end
%%step4: vlaues are sorted with repeat val eliminated
AsortedWihtoutRepeat = Asorted.*Mask
% step5: Now we need to rewrite zero values with intermediate increased values
% RewritedMatrix = ??
Step4 results:
AsortedWihtoutRepeat =
1 0 0 0 2 0 3 4 5 0 7
1 0 3 0 4 5 0 6 7 8 9
Now I need to rewrite zeros by intermediate values, for example:
AsortedWihtoutRepeat(1,2) = value been value bigger than 1 and lower than 2. AsortedWihtoutRepeat(1,3) = value been value bigger than 1 and lower than 2.
And I need that position (1,2) and (1,3) will be increased values.
Step5 example:
RewritedMatrix =
1 1.1 1.5 1.7 2 2.5 3 4 5 6 7
1 2 3 3.5 4 5 5.5 6 7 8 9
0 Commenti
Risposta accettata
Guillaume
il 5 Set 2018
Modificato: Guillaume
il 5 Set 2018
Another option is to use fillmissing with the 'linear' option (which under the hood does call interp1 similar to Stephen's answer). You can simplify your code to:
A = [ 1 1 1 1 2 2 3 4 5 7 5;
3 3 6 5 4 1 5 7 8 9 1 ] ;
%step 1:
Asorted = sort(A, 2);
%step 2, 3 and 4 combined:
Asorted([false(size(A, 1), 1), diff(Asorted, [], 2) == 0]) = NaN;
%step 5:
Afinal = fillmissing(Asorted', 'linear')' %has to transpose since fillmissing works on columns
Note that if you worked on columns rather than rows, the whole code would be simpler:
A = [ 1 1 1 1 2 2 3 4 5 7 5;
3 3 6 5 4 1 5 7 8 9 1 ]'; %transposed A
Asorted = sort(A);
Asorted([false(1, size(A, 2)); diff(A) == 0]) = NaN;
Afinal = fillmissing(Asorted, 'linear')
Più risposte (1)
Stephen23
il 5 Set 2018
Modificato: Stephen23
il 5 Set 2018
>> vec = [1,0,0,0,2,0,3,4,5,0,7]; % one row of the matrix
>> idx = vec~=0;
>> out = interp1(find(idx),vec(idx),1:numel(vec))
out =
1.0000 1.2500 1.5000 1.7500 2.0000 2.5000 3.0000 4.0000 5.0000 6.0000 7.0000
Put that in a for loop and you can apply it to each matrix row.
5 Commenti
Stephen23
il 5 Set 2018
Modificato: Stephen23
il 5 Set 2018
@Cesar Antonio Lopez Segura: somehow you have changed your algorithm. In your original question you gave this example matrix:
AsortedWihtoutRepeat =
1 0 0 0 2 0 3 4 5 0 7
1 0 3 0 4 5 0 6 7 8 9
But when I actually run your code I get this matrix:
AsortedWihtoutRepeat =
0 0 0 1 0 2 3 4 0 5 7
0 1 0 3 4 0 5 6 7 8 9
Given your different input arrangement, there is no reason to expect the same output.
Vedere anche
Categorie
Scopri di più su Descriptive Statistics in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!