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
Related
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
How could I correlate the first (also second and third, etc) line of each function together? I meant I want to pick up the value of the first line of function 1, first line of function 2 and so on... Specially number of these lines (between two #...#) are not always equal.
Let's say the values between (# -#) is one matrix. I see that the problem is I need to break them down into different matrix and then equalize all these matrix to the same size by adding NaN values and then reconstruct them again. That is what I think but I dont know how to ask Matlab do these tasks.
Would you please give me a help?
Thank you very much !!
I'm asked to do a program that asks for two numbers, the first variable for example is 4 and the second is 5, i need the program to show the first variable as 5 and the second one as 4, so i need the variables to exchange their values
Thanks :DDDD
I think you are asking for number swapping logic.So you can use two logic to swap the number
1. By using 3rd variable.
2. Without using 3rd variable.
Their respective logic's are as follows.
Suppose you have x=4,y=5;
take 3rd variable like temp;
temp=x; x=y; y=temp;
and 2nd logic.
x = x+y; y=x-y; x=x-y;
Such questions are usually asked to reduce the space complexity and to do so we take the following approach:
a=a+b;
b=a-b;
a=a-b;
for example we take a=4 and b=5
a becomes 9
b becomes 4
a becomes 5
finally a=5 and b=4 (swapped)
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.