This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Determining the number of occurrences of each unique element in a vector
I've the following array:
v = [ 1 5 1 6 7 1 5 5 1 1]
And I need to count the values and show the number that has more appearances.
From the example on the top, the solution would be 1 (there are five 1's)
Thanks in advance
Use mode.
If you need to return the number of elements as well, do the following:
m = mode(v);
n = sum(v==m);
fprintf('%d appears %d times\n',m,n);
Another method is using the hist function, if you're dealing with integers.
numbers=unique(v); %#provides sorted unique list of elements
count=hist(v,numbers); %#provides a count of each element's occurrence
Just make sure you specify an output value for the hist function, or you'll end up with a bar graph.
#Jacob is right: mode(v) will give you the answer you need.
I just wanted to add a nice way to represent the frequencies of each value:
bar(accumarray(v', 1))
will show a nice bar diagram with the count of each value in v.
Related
I have encountered this problem during my preparation for the interview, I can't think of an elegant way to solve it:
Given an array of positive integers and target K, asking what are the combinations can satisfy K? We assume all integers <= K
example:
given: [5,4,3,5,2,1,4,8,10]
K = 10
10
5+5 = 10
2+8 = 10
4+4+1 = 9
3 *because it can't be add to any conbination and still less than or equal to k
so one of the answers is [10],[5,5],[2,8],[4,4,1],[3]
but we want minimum numbers of combination, in this case, we get 5
My apology on the tags, I don't know which is the best way to solve it and I have to add tags to post the question
This question already has answers here:
Getting the indices of several elements in a NumPy array at once
(5 answers)
Closed 2 years ago.
Assume I have an array:
a = np.array([1,2,3,4,5])
Now I want to find the indices of elements in this array corresponding to the values given by another array input:
input = np.array([2,4,5])
The expected result should be:
result = [1,3,4]
A boolean mask, which is true for element indices 1,3,4 would also be fine.
I do not want to use looping to solve this. I assume that a possible solution has to do with the numpy where() function, but using this one, I am only able to compare the entries of array a with one element of array input at a time. Because the length of input might differ, I cannot really use this approach. Do you have any other ideas?
Thanks in advance.
np.where(np.in1d(a, inp))[0]
or:
np.isin(a, inp).nonzero()[0]
or as suggested here:
sorter = np.argsort(a)
sorter[np.searchsorted(a, inp, sorter=sorter)]
output:
[1 3 4]
np.where(np.in1d(a, inp))[0] np.where(np.in1d(a, inp))[0]
This question already has answers here:
Get a number from an array of digits
(3 answers)
Closed 3 years ago.
I really like Julia's Digits function which will return an array of the individual digits that make up the input integer (See code example). My question is, once you have this array, is there an easy way to join the individual values back into the original value passed into digits?
Now, I suppose I could just take each integer's index value and multiply by it's corresponding power of 10 and modify the array in place. Then I could use sum on the array, but is there a better way to do it?
I.E
function getDigits(x)
return digits(x)
end
Julia> getDigits(1234)
4-element Array{Int64,1}:
4
3
2
1
function joinDigits(digitArray)
for i in 0:length(digitArray)-1
digitArray[i+1] = digitArray[i+1] * 10 ^ i
end
return sum(digitArray)
Julia> joinDigits([4,3,2,1])
1234
foldr((rest, msd) -> rest + 10*msd,x) is a one liner that does the same thing. It's terse, but probably not as clean as the for loop.
This question already has answers here:
On shape-agnostic slicing of ndarrays
(2 answers)
Closed 6 years ago.
Suppose A is multi-dimensional array (MDA) of size 3,4,5 and B is another MDA of size 3,4,5,6.
I know A(1,:,:) or B(1,:,:,:) can both extract their elements along the first dimension.
I now need to write a general program to extract the k-th dimension from a MDA without knowing its size.
For example, the MDA C has 6 dimension: 4,5,6,7,8,9 and I want an extraction C(:,:,k,:,:,:).
Sometimes, the MDA 'D' has 4 dimension: 3,4,5,6 and I want another extraction D(k,:,:,:).
That is, my problem is the numbers of colon is varying because of the dimension.
Thanks in advance
You can use string arrays to index the array dynamically:
function out = extract(arr,dim,k)
subses = repmat({':'}, [1 ndims(arr)]);
subses(dim) = num2cell(k);
out = arr(subses{:});
where dim is the dimension in which you want to select and k is an index within that dimension.
I have used a code from this answer:
https://stackoverflow.com/a/27975910/3399825
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Is this C implementation of Fisher-Yates shuffle correct?
In order to simulate different order of input sequence into my application, I would like to generate a list of randomize sequence for an array input. For example, given an arr[10], the default sequence is 0,1,..,8,9. However, I'd like to manipulate the sequence into random order, for eg, 2,4,5,1,9,0,3,7,8,6.
I think rand() will generate a random value between 0-9, but it doesn't assure that each element is generated at least once. In such case, I am thinking about below pseudo, but is there a better way to generate random input sequence, and assure each number within the given range is generated at least once?
round #1:
generate a random number within 0-9.
let say 2 is selected
round #2:
generate a random number within 0-1
generate a random number within 3-9
let say 0 and 4 are selected
round #3:
generate a random number within 1
generate a random number within 3
generate a random number within 5-9
let say 1, 3, 7 are selected
round #4:
generate a random number within 5-6
generate a random number within 8-9
continue until 10 numbers are selected
Look at Fisher-Yates shuffle algorithm here for your requirement.
Here is another approach:
Create array of 10 elements and fill it with the values you want to randomize (0, 1, ..., 9).
Iterate for k from 9 down to 0 and pick a random number index = rand(k).
Swap the element at position k with the element at position index.
At the end your array will contain a random permutation of the original values, which is exactly what you wanted.