Compare and modify random vector with evenly spaced vector

3 次查看(过去 30 天)
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 个评论
John D'Errico
John D'Errico 2019-9-1
编辑:John D'Errico 2019-9-1
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.
Mukund
Mukund 2019-9-1
The said change is made. However, the expectation is to obtain the final result. How to do that.

请先登录,再进行评论。

采纳的回答

John D'Errico
John D'Errico 2019-9-1
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.

更多回答(1 个)

Mukund
Mukund 2019-9-1
编辑:Mukund 2019-10-1
Dear John, We generally perform search on google and mathwork site, before posting. 'Matlab help' sometimes lacks the example demonstration that is required by the user. This is where such forums become useful for Matlab users. Thanks for your effort.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by