I've got fixed numbers: -3, -1, 1, 3. How do I randomly generate a matrix like the following?
1 -1 -3 -1
3 -3 -3 3
3 3 1 -1
3 -3 3 -1
Use randi to create random index values into your vector of possible values:
x = [-3 -1 1 3]
y = randi(length(x),[5 5]);
y = x(y);
Although #nkjt's answer is probably the way to go, if you have the Statistics Toolbox you can simplify a little using randsample (with replacement):
result = NaN(3,6); %// define required size
result(:) = randsample([-3 -1 1 3], numel(result), true);
Or, if the original numbers are equally spaced as in your example, you can solve it in one line:
result = 2*randi(4,[3 6])-5; %// "2" and "5" as per your original values
You can use
randperm Random permutation
Related
i was using the function "resizem" to expand the size of my matrix, without erasing the data. recently, after using the function, it sends me an error that the function cannot use integers of type double. i have tried to use abs().^2 on my matrix but it didn't work.
is there any other function that can do the same?
note: i need the data to not change, so if i have an matrix
A = [-1 0 2; 4 1 2] i want it to become a 5x5 matrix like so:
A = [-1 -0.5 0 1 2; 4 2.5 1 1.5 2]
Also, I have tried the function 'imresize', and gives me the same problem as the resizem. (I own both the tool box, and my matrix is in type double)
If A = [-1 0 2; 4 1 2] you could do something like A = [A, [1, 2; 1.5 2]] to get A = [-1 -0.5 0 1 2; 4 2.5 1 1.5 2].
I would try to go for imresize (which is part of the Image Processing Toolbox). I never used those functions, but looking at the error you are receiving, which is related to variable types, the new option should provide more flexibility. In fact, while resizem returns double precision output, imresize returns an output of the same type as the input.
A = [-1 0 2; 4 1 2];
imresize(A,[2 5]);
The example shows how to use it. I also tested both outcomes:
A = [-1 0 2; 4 1 2];
A_imr = imresize(A,[2 5]);
A_rem = resizem(A,[2 5]);
This is the result for imresize:
A_imr:
-1,08000000000000 -0,720000000000000 -1,77635683940025e-15 1,29600000000000 2,16000000000000
4,24000000000000 2,82400000000000 1,00000000000000 1,48000000000000 2,08000000000000
This is the result for resizem:
A_rem :
-1 -1 0 2 2
4 4 1 2 2
For further tinkering, try to play with scale and method arguments. In imresize, for example, you need to specify the appropriate interpolation kernel to achive averaging.
I have some data like
interesting_data =
-0.5665 -0.5329 -0.2251 0.2251 0.5329 0.5689
It is obvious there is a zero cross between index 3 and 4. I found I can use this function to get the zero cross point of pchip interpolation.
fzero(#(xi)interp1(1:1:length(interesting_data),interesting_data,xi,'pchip'),3)
So I believe matlab have a behind function for the pchip interpolation between [3,4]. Suppose the pchip module will generate the equation as y = ax^2+bx+C, x is within [3,4[, or something like that. I hope to know the fomular, then I can calculate the derivation at the zero-crossing position, in this case, x=3.5.
Is there any way to do that? Or any function to tell the derivation directly from the hidden equation?
Why not just see when the sign flips. Here is your array and signs:
array =[ -0.5665 -0.5329 -0.2251 0.2251 0.5329 0.5689];
sign(array)
ans =
-1 -1 -1 1 1 1
When the sign changes, it will return 2 or -2
diff(sign(array))
ans =
0 0 2 0 0
So just find the nonzeros
find(abs(diff(sign(array))))
ans =
3
Lets try it for another array
array=randn(1,10)
array =
1.42837429557213 1.40677399588789 0.257616732589725 -0.93068654061524 0.391997711945428 1.53494945223268 -1.03683690895487 0.126547794228052 -0.394102426308772 -0.562889126426436
diff(sign(array))
ans =
0 0 -2 2 0 -2 2 -2 0
find(diff(sign(array)))
ans =
3 4 6 7 8
i have a question regarding histc:
I choose the max and min of a sorted signal as my range.
ma = ssigPE(end);
mi = ssigPE(1);
range = mi:ma;
[bincountsO,indO2] = histc(ssigPE, range);
so the range i get back is:
range = [-1.097184703736132 -0.097184703736132 0.902815296263868]
my problem is that just 2 bins get develop, so bincountsO has 2 bins
and indO2 has values as 0, 1 and 2
What am I doing wrong? I guess I m using the range wrong. I read the text here:
http://de.mathworks.com/help/matlab/ref/histc.html#inputarg_binranges
but I don't get it.
The bin ranges tell you where do bins start and stop. So a value of [0 1 2 7]for example, will give 3 bins: [0 1] , [1 2] , [2 7]
In matlab if you do mi:ma it will create an array from the value mi to ma with a step of 1. With your values, that gives just 3 values, hence 2 bins. There are 2 ways of creating a given step size length vectors.
Step size if 100 as an example
range=mi:(ma-mi)/100:ma;
alternatively, and way clearer
range=linspace(mi,ma,100)
I'm setting up a script and I want it to systematically go through ALL POSSIBLE 2x2, 3x3, and 4x4 matrices modulo 2, 3, 4, 5, 6, and 7. For example, for modulus 2 in a 2x2, there would be 16 possibilities (4^2 because there are 4 positions with 2 possibilities each). I'm having trouble getting MATLAB to not only form all these possibilities but to put them through my script one at a time. Any thoughts?
Thanks!
This solution uses allcomb from matlab file exchange.
%size
n=2
%maximum value
m=2
%generate input for allcomb
e=cell(1,n^2)
e(1:end)={[0:m-1]}
%generate all combinations.
F=reshape(allcomb(e{:}),[],n,n)
F is a 3D-Matrix, to get the first possibility use:
squeeze(F(1,:,:))
A slight generalization of this Q&A does the job in one line:
r = 2; %// number of rows
c = 2; %// number of columns
n = 2; %// considered values: 0, 1, ..., n-1
M = reshape(dec2base(0:n^(r*c)-1, n).' - '0', r,c,[]);
Result for r, c, n as above:
M(:,:,1) =
0 0
0 0
M(:,:,2) =
0 0
0 1
...
M(:,:,16) =
1 1
1 1
I would like to find out if there is a function that can generate random numbers from a set of numbers in Matlab? For instance say I have the set [-1 1]. How can I generate numbers from that set? I have tried to use randi([-1,1]) but that obviously will generate the numbers -1,0 and 1 and I would like to generate numbers uniformly WITHOUT the 0 and only include -1 and 1. I apologise in advance if this seems like a trivial question but I can't seem to find the answer.
Thanks for any help you can provide.
Edit: I've found a simple solution for the above: (randi([0 1])*2) - 1. This only solves this problem and not a generation of numbers from a specified set.
Well, is there a function that can generate integers uniformly from the set 1:2, or, in general, from the set 1:n, where n is the number of elements in your set? (Yes)
If the above answer was yes, then can those numbers be used as an index into the generated set? (Yes)
A = [-1 1];
n = numel(A);
A(ceil(rand*n))
Will it sample randomly and uniformly? (Yes)
Generate a uniform random integer in the range 1 to N (where N is the size of the set). Then use that to index into the set.
>> S = sign(rand(1,10) - .5); S(S ~= 0)
ans =
1 -1 1 1 -1 1 -1 -1 1 -1
This is obviously specialized to your specific example, but the same general concept can be applied.
EDIT: Here is a more general example:
>> Set = [1 10 100]
Set =
1 10 100
>> Set(randi([1 3], 10, 1))
ans =
100 100 100 100 100 10 10 1 100 1