How can i make this code dynamic?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
shubham shubham
am 29 Okt. 2020
Kommentiert: Walter Roberson
am 29 Okt. 2020
function gr = semm(p,k)
if nargin < 2
b=[30,15,6];
end
gr=s;
gr(:)=4;
gr(p> 100 ) = "Not valid output";
gr(p >= k(3) ) = 3;
gr(p >= k(2) ) = 2;
gr(p >= k(1) ) = 1;
How can I make the above code dynamic so that It doesn't have k(1),k(2),k(3) and it can see at run time the vector k and decide which gr to assign to each element in p?
P.s- any help is appreciated.
3 Kommentare
Walter Roberson
am 29 Okt. 2020
Is there a guarantee that k is sorted in descending order? If it is not sorted, then should the output be as-if it were sorted, or should the output be according to the logic you posted, where the first value in k for which the conditions hold determines the output? For example if k were [15,30,6] then should input 33 be output 2 (position 2 of k holding the greatest value that does not exceed the input), or should it be output 1 (because even though 33 > 30 is true and the most restrictive, your logic would test >= 15 after that and replace the 2 with a 1) ?
Akzeptierte Antwort
Walter Roberson
am 29 Okt. 2020
bin = discretize(p, [-inf; flipud(k(:)); 100; inf]);
map = [num2cell(length(k)+1:-1:1), {"Not valid output"}];
gr = map(bin);
The output is a cell array in which each entry is either a single number or the scalar string "Not valid output", such as
{["Not valid output"]} {[4]} {[4]} {[4]} {[1]} {["Not valid output"]} {["Not valid output"]} {[4]} {[3]} {["Not valid output"]} {[2]}
You could also consider
gr = discretize(p, [-inf; flipud(k(:)); 100; inf], 'categorical', [string(length(k)+1:-1:1), "Not valid output"])
Not valid output 4 4 4 1 Not valid output Not valid output 4 3 Not valid output 2
However, unless you go through the same kind of map layer, those 4's and 1's are for display purposes, and do not correspond to the internal bin categorical numbers:
>> uint8(gr(1:11))
ans =
1×11 uint8 row vector
5 1 1 1 4 5 5 1 2 5 3
2 Kommentare
Walter Roberson
am 29 Okt. 2020
discretize is a function that takes input data and also takes an increasing vector that represent edges for bins. For each input value, the code returns the number of the first bin such that edge(bin) <= x < edge(bin+1), except that the final test is <= edge(end) instead of just < .
Your rules have several cases when you map them out
- any value less than the last k is assigned one more than the index of the last k. This is a bit odd for a rule but whatever. This rule implies a bin for -infinity to the smallest k entry
- when you list the k values in increasing order they are edges suitable for discretize to use: the output number is to be the largest index that the input is at least as large as the k value. But because we inserted a bin from -infinity to the smallest k, we will need to account for that extra bin in post-processing
- any value 100 and up is to generate the error output. Here my code has a potential problem as your problem statement has p>100 for the error but I implemented p>=100. If that is a problem then replace the 100 with 100*(1+eps) as that should work out to the smallest representable number greater than 100.
Now we have a bin number that is 1 for the case that you wanted to assign 4 (one more than the length of k) to, and the bin number is 2 for the smallest k, which you want to assign length(k) to, bin number 3 for the case you want to assign length(k)-1 to, and so on with bin number equal to length(k) + 1 for the case you want to assign 1 to, and with bin number length(k)+2 for the 100 to infinity case that you want to assign an error message to.
There are a number of different ways you can move from the bin numbers emitted by discretize into output values. One of the fastest of those ways is to create a vector of what each value is to map on to. It happens that the mapping list is by decreasing value starting from length(k)+1 (corresponding to values not changed away from 4 in your code) and that it is the last bin number that you want the error message for.
We had to use a cell array because error messages cannot go into a numeric matrix.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!