rand(3,4)
generates the following 3x4 matrix for me:
0.4229 0.4709 0.6385 0.3196
0.0942 0.6959 0.0336 0.5309
0.5985 0.6999 0.0688 0.6544
How can I limit the values generated to a specific range? Like restricting them to values between 1 and 100. And how can I specify whether I want the random numbers to be float or int?
You can multiply by the range you want, and add the minimum value. So, to get a matrix with values in the range [1, 100], do:
1 + 99*rand(3,4)
If you want single or something else, use the typename argument to specify this.
If you want integers, use randi:
1 + randi(99,3,4)
Related
So I have a matrix A. And I can find the values of greater than 7 with logical indexing of A>7.
How can I then, replace all the values of A>7 by the numbers divided by 2?
I tried:
A(A>7) = [num1/2, num2/2, etc]
But I'd want the math done without me inputting the nums/2 values to be replaced accordingly.
You can do it easily by using the same indices likes the following:
indices = A > 7;
A(indices) = A(indices)/2;
I have a data like below:
49.6 46.1
49.65 46.3
50.1 47.03
50.2 47.06
51.35 46.027
51.36 46.20
I want to find the mean values of both column based on specific range of first column. for example in range of [49-50) I should calculate the mean values of the first column and mean of corresponding values in the second column. In this example the sub-array (first column only) with numbers
49.6
49.65
will be in range of [49-50) so I want to find the mean value for them and the mean value of the corresponding values in the 2nd column.
The range would be like 49:1:100. The code below doesn't work properly.
for i=49:1:100
meanWithinRange(i) = mean(data(i,1));
end
I think you are looking for logical indexing.
First, create a logical array for the in-range values of column 1:
A=[49.6 46.1
49.65 46.3
50.1 47.03
50.2 47.06
51.35 46.027
51.36 46.20];
I = A(:,1)>=49 & A(:,1)<50;
I is a logical column vector, and is true for the rows that are in range. You can use this to index the rows you want:
>> A(I,:)
ans =
49.6000 46.1000
49.6500 46.3000
So now you can simply compute the mean of this result:
>> mean(A(I,:))
ans =
49.6250 46.2000
I need obtain a vector with n numbers disposed in random order in MatLab. How can I do it?
If I use, for example, randi(10,1,10), I will obtain random values, but I have no garantee that all the numbers will be in the sequence.
For example:
I need a vector with all the numbers 1,2,3,4,5,6,7,8,9,10, disposed in a random order, like in 2,5,3,6,8,10,4,7,9,1.
It appears that you are looking for the randperm function. From the docs:
p = randperm(n) returns a row vector containing a random permutation of the integers from 1 to n inclusive.
Your example would be randperm(10).
There is an optional second argument that you could pass in to determine how many elements will be chosen. For example, randperm(10, 5) would choose five random numbers from 1 to 10.
You can also use the results of randperm to shuffle or select from an arbitrary vector. Say you wanted the numbers 101 to 110 in random order instead of 1 to 10:
nums = 101:110;
nums = nums(randperm(numel(nums)));
i want to know the coding to find the maximum value, over a certain range.
i already coded like below.
f=f'
ac_yyyy_f=ac_yyyy_f'
[row,col] = ind2sub(size(ac_yyyy_f),find(ac_yyyy_f==max(ac_yyyy_f)))
but the problem is, sometimes the maximum value of Y axis choosen by my code is not what i want.
the X axis has the range of 0 to 100000 and i want the maximum between 20000 to 100000. the problem is sometimes the max value show up at the range of 0 to 20000.
How can i figure this out?
Use the max() function:
%let R be your range of values
R = [2 1 7 4];
[value, index] = max(R);
In the above example, value will be 7 and index will be 3
For more info: http://fr.mathworks.com/help/matlab/ref/max.html
I'm using a vector of random integers to stand in for your function output.
a = floor(rand(1,100000)*100);
[val, idx] = max(a(20000:100000));
You want to use the max function here to find the maximum value rather than find.
Now, the other part of the task is getting the max value from a certain part of your matrix. You can pass just a subset of a vector or matrix to a function by indexing it with a range of values. Note that idx gives you the position of val within a(20000:100000). If you need the position within a, you need to use idx+19999.
Also, you should take a look at the matrix indexing reference—there are many different and fun ways to index a matrix—because indexing is one of the most important features of matlab.
Here's the reference for the max function:
http://www.mathworks.com/help/matlab/ref/max.html
And the reference for indexing:
http://www.mathworks.com/help/matlab/math/matrix-indexing.html
You can use the max function with a subset of your array. It will return the maximum value, as well as the index where it is located. Be sure to correct the index it returns you based on your desired range. Like this:
%//create an array of 100,000 values to play with.
f=floor(rand(100000,1).*100);
%//find the max between f(20000) and f(100000)
[myMax, I] = max( f(20000:100000) );
%//correct the index based on where we started looking
%//for the max. Subtract 1 because it's MATLAB!
myIndex = I+20000-1;
This results in:
>> myMax
myMax =
99
>> myIndex
myIndex =
20045
>> f(myIndex)
ans =
99
very basic question: How can I find the highest or lowest value in a random matrix.
I know there is a possibility to say:
a = find(A>0.5)
but what I'm looking for would be more like this:
A = rand(5,5)
A =
0.9388 0.9498 0.6059 0.7447 0.2835
0.6338 0.0104 0.5179 0.8738 0.0586
0.9297 0.1678 0.9429 0.9641 0.8210
0.0629 0.7553 0.7412 0.9819 0.1795
0.3069 0.8338 0.7011 0.9186 0.0349
% find highest (or lowest) value
ans = A(19)%for the highest or A(7) %for the lowest value in this case
Have a look at the min() and max() functions. They can return both the highest/lowest value, and its index:
[B,I]=min(A(:)); %# note I fixed a bug on this line!
returns I=7 and B=A(7)=A(2,2). The expression A(:) tells MATLAB to treat A as a 1D array for now, so even though A is 5x5, it returns the linear index 7.
If you need the 2D coordinates, i.e. the "2,2" in B=A(7)=A(2,2), you can use [I,J] = ind2sub(size(A),I) which returns I=2,J=2, see here.
Update
If you need all the entries' indices which reach the minimum value, you can use find:
I = find(A==min(A(:));
I is now a vector of all of them.
For matrices you need to run the MIN and MAX functions twice since they operate column-wise, i.e. max(A) returns a vector with each element being the maximum element in the corresponding column of A.
>> A = rand(4)
A =
0.421761282626275 0.655740699156587 0.678735154857773 0.655477890177557
0.915735525189067 0.0357116785741896 0.757740130578333 0.171186687811562
0.792207329559554 0.849129305868777 0.743132468124916 0.706046088019609
0.959492426392903 0.933993247757551 0.392227019534168 0.0318328463774207
>> max(max(A))
ans =
0.959492426392903
>> min(min(A))
ans =
0.0318328463774207
Note that this only works for matrices. Higher dimensional arrays would require running MIN and MAX as many times as there are dimensions which you can get using NDIMS.
Try this out
A=magic(5)
[x,y]=find(A==max(max(A))) %index maximum of the matrix A
A_max=A(x,y)
[x1,y1]=find(A==min(max(A))) %index minimum of the matrix A
A_min=A(x1,y1)