Compare and modify random vector with evenly spaced vector
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mukund
am 1 Sep. 2019
Bearbeitet: Mukund
am 1 Okt. 2019
A is vector randomly generated and length varies from 5-7. B is vector of evenly spaced values of length 10. Every element of A is to be checked with every class (bin) of element of B.
A=[0.1, 0.21, 0.346, 0.59, 0.744, 1]; % actually created with random numbers
B=linspace(min(A),max(A),10); % linear 10 pieces
Value 0.1 of A is present in B (say, class 0.1-0.2), hence, it is kept in A. If any value in A does not belong to the class in B, then it is asigned as zeros. Finally, the length of A would enhance to that of B. The expected result would be as like:
A=[0.1,0.21,0.346,0,0.59,0,0.744,0,1];
2 Kommentare
John D'Errico
am 1 Sep. 2019
Bearbeitet: John D'Errico
am 1 Sep. 2019
A BAD way to code:
B=min(A):1/9:max(A); % linear 10 pieces
A better way to code, for the same target result:
B=linspace(min(A),max(A),10); % linear 10 pieces
Learn to use the tools in MATLAB properly, here, linspace is a terribly valuable tool.
Why is linspace better? Because if you explicitly want 10 values that are linear over that interval, linspace gives you them, and does so accurately. You only need to indicate the NUMBER of elements, thus an integer value.
What happens when you use colon, and you specify the increment? Here, you need to be careful, to compute the correct spacing yourself.
A=[0.1, 0.21, 0.346, 0.59, 0.744, 1]
A =
0.1 0.21 0.346 0.59 0.744 1
B=min(A):1/9:max(A)
B =
0.1 0.21111 0.32222 0.43333 0.54444 0.65556 0.76667 0.87778 0.98889
length(B)
ans =
9
B(end)
ans =
0.988888888888889
B=linspace(min(A),max(A),10)
B =
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
length(B)
ans =
10
Do you see what happened? The increment that you used was not in fact correct. The last element of B when you created it using colon was
B=min(A):1/9:max(A);
B(end)
ans =
0.988888888888889
It was not 1, because 1/9 is not the correct increment to achieve that result. You made an error of thought, using the wrong increment.
Again, learn to use linspace, because that makes your code better. It prevents you from making simple mistakes.
Akzeptierte Antwort
John D'Errico
am 1 Sep. 2019
Again, learn to use the tools in MATLAB. The first was linspace. A second is discretize.
A=[0.1, 0.21, 0.346, 0.59, 0.744, 1];
B=linspace(min(A),max(A),10);
C=zeros(size(A));
ind = discretize(A,B);
C(ind) = A;
I'd suggest what you really need to do is to read the getting started tutorials, especially since you seemed not to know about linspace.
A second thing is to just start reading the help for the various tools in MATLAB. For example, if you try this:
help datafun
you ill find the names of many functions in MATLAB that will be useful to you. Click on the names that seem like they might be useful to YOU. See how to use those tools.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical 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!