I have a matrix <1x100> named test containing values such as: 10,10,30,50,50,30,30,10,40,...
Is it possible to check whether the matrix contains at least 3 variations of numbers, for the example I showed it would evaluate to true since we have 10, 30, 50. In another example: 10,10,10,20,10,20,10,10... it should give false since we only have 10 and 20.
Help is much appreciated!
Try unique command in Matlab. It will give you unique elements in the array and then you can check if its length is 3 or more as per your criteria.
length(unique(a))
length will give you the number of unique elements... This will help to check if it is 3 or more
Related
The matrix <1x500> consists of different values, now I want to check if any of the values in the matrix occurs at least 3 times or more.
if (val occurs 3 times or more)
do
Help is very appreciated!
Another option from #KiW answer for when you need to know all the values that do appear at least 3 times is:
uniqA=unique(A);
counts=histcounts(A,[uniqA inf]);
vals_that_are_bigger=uniqA(counts>=3);
To check if any of them are bigger than 3, just
if any(counts>=3)
if numel(find(matrix)==val)>3
whatever you want to do
end
My goal is to create a random, 20 by 5 array of integers, sort them by increasing order from top to bottom and from left to right, and then calculate the mean in each of the resulting 20 rows. This gives me a 1 by 20 array of the means. I then have to find the column whose mean is closest to 0. Here is my code so far:
RandomArray= randi([-100 100],20,5);
NewArray=reshape(sort(RandomArray(:)),20,5);
MeanArray= mean(transpose(NewArray(:,:)))
X=min(abs(x-0))
How can I store the column number whose mean is closest to 0 into a variable? I'm only about a month into coding so this probably seems like a very simple problem. Thanks
You're almost there. All you need is a find:
RandomArray= randi([-100 100],20,5);
NewArray=reshape(sort(RandomArray(:)),20,5);
% MeanArray= mean(transpose(NewArray(:,:))) %// gives means per row, not column
ColNum = find(abs(mean(NewArray,1))==min(abs(mean(NewArray,1)))); %// gives you the column number of the minimum
MeanColumn = RandomArray(:,ColNum);
find will give you the index of the entry where abs(mean(NewArray)), i.e. the absolute values of the mean per column equals the minimum of that same array, thus the index where the mean of the column is closest to 0.
Note that you don't need your MeanArray, as it transposes (which can be done by NewArray.', and then gives the mean per column, i.e. your old rows. I chucked everything in the find statement.
As suggested in the comment by Matthias W. it's faster to use the second output of min directly instead of a find:
RandomArray= randi([-100 100],20,5);
NewArray=reshape(sort(RandomArray(:)),20,5);
% MeanArray= mean(transpose(NewArray(:,:))) %// gives means per row, not column
[~,ColNum] = min(abs(mean(NewArray,1)));
MeanColumn = RandomArray(:,ColNum);
I am having problem with MATLAB's maximum function. What I am supposed to do is to replace the maximum value of an array with a number. However, when there are more than one maximal value, the program updates all of them simultaneously. Is there a way to make it do it one by one? The order of replacement is not important; it can be done arbitrarily. The only important thing is to have MATLAB doing it one by one.
Thank you in advance.
The second output of max returns one index:
a=[5,5];
[b,idx]=max(a)
c=b-2;
a(idx)=c
When you say more than 1 maximum value, I assume you are talking about a matrix where max function operates on every column?
You can do the following:
a = [1 1 2;5 5 7; 3 2 9]
Obviously, the maximum value is going to be 9, but if you do the following:
max(a)
The result will be:
5 5 9
Based on each column.
The following may work for you?
max(a(:)) % Maximum value from a matrix (rerranged into 1 column)
you can do the same for the min function.
I want to generate a matrix of random numbers (normrnd with mean == 0) that satisfy the following constraints using MATLAB (or any other language)
The sum of the absolute values in the matrix must equal X
The largest abs(single number) must equal Y
The difference between the number and its 8 neighbors (3 if in corner, 5 if on edge) must be less than Z
It would be relatively easy to satisfy one of the constraints, but I can't think of an algorithm that satisfies all of them...
Any ideas?
I am not sure whether to edit my post or to reply here, so I am editing... #MZimmerman6, you have a point. Though these constraints won't produce a unique solution, how would I obtain multiple solutions without using rand?
A very simply 3 x 3 where 5 is the max element value, 30 is the sum, and 2 is the difference
5 4 3
4 4 2
3 2 3
Rody, that actually may help...I need to think more :)
Luis ...Hmmm...why not? I can add up the absolute value of a normally distributed sample...right?
Here is an algorithm to get the 'random' numbers that you need.
Generate a valid number (for example in the middle)
Determine the feasible range for one of the numbers next to it
If there is no range, you go to step 1, otherwise generate a number and continue
Depending on your constraints it may take a while of course. You could add an other step to see if changing the existing numbers would help before going back to step 1.
I have created a vector containing zeros and 1's using the following command in a for loop.
G(:,i)=rand(K,1)<rand;
Since this is part of a larger problem at a particular stage I need to count the number of 1's that are present in each column.
I have tried to find the count using a for loop which is very messy and takes too long.
I found that histc can be used for this but I get an error
histc(G(:,1),1)
First input must be non-sparse numeric array.
Is there a better way to do this or am I missing something here ?
If you have a matrix G containing zeroes and ones, and you want to know how many ones are in each column, all you need is SUM:
nZeroes = sum(G);
This will give you a vector containing a total for each column in G.