How can I count the specific values of an array in MATLAB? - arrays

Here is a 10x10 array arr.
This arr has 100 elements. And it has distribution from -10 to 10 and there are 5 0-value.
I did this code because I wanted to know the number of 0.
count = 0;
for i = 1: 10
     for j = 1: 10
         if (arr (i, j) == 0)
             count = count +1;
end
     end
end
Logically, count should be 5 in MATLAB's workspace. and i and j are 10.
However, when I run the code, count is 0.
This code can not count the numbers.
How can I count the numbers?

You can just use nnz to get the number of non-zero elements in a logical array, so the number of elements in arr with the value 0 is
count = nnz( arr == 0 );
Please read Why is 24.0000 not equal to 24.0000 in MATLAB? for information about comparisons of floating point numbers, you may need to do
tol = 1e-6; % some tolerance on your comparison
count = nnz( abs(arr) < tol ); % abs(arr - x) for values equal to x within +/-tol

correct me if I'm wrong but it sounds like you want the number of occurrences of the numbers in your vector, here's an alternative if that is the case:
arr=[1 2 2;3 3 3;5 0 0;0 0 0]; % example array where 1,2,3 occur 1x,2x,3x and 5=>1x, 0=>5x
[x(:,2),x(:,1)]=hist(arr(:),unique(arr(:)));
outputs sorted category as first column, occurrences as 2nd column:
x =
0 5
1 1
2 2
3 3
5 1

Related

Number of ways to fill an array of size n , such that the mexium is greater than every element of the array?

We have given an empty array of size n , we need to fill it with natural numbers (we are allowed to repeat).
The condition that must follow is the mex of the array must be greater than all the elements we fill in the array .
Can someone pls help me with the number of ways to do so ?
(Different arrangements of same set of numbers are also considered distinct)
PS:- by mex of a sequence I mean the smallest non negative number that doesn't occur in the sequence
Number of such arrays is equivalent to the number of ordered distributions of values 1..N into buckets (so [A],[B,C] and [B,C][A] are distinct ones). And number of such distributions is described by ordered Bell numbers 1,3,13,75....
Example for N=3
1 1 1 //1 permutation
1 1 2 //3 permutations
1 2 2 //3 permutations
1 2 3 //6 permutations
//13 variants
Generation of distributions themselves for reference. Note that for N values every value might fall into part 1..K, where K is in range 1..N, so numbers of parts corresponding to all values form continuous sequence without holes (cf. your mex)
To calculate number of such distributions, we can use recurrence from Wiki, Python code:
def cnk(n, k):
k = min(k, n - k)
if k <= 0:
return 1 if k == 0 else 0
res = 1
for i in range(k):
res = res * (n - i) // (i + 1)
return res
def orderedbell(n):
a = [0]*(n+1)
a[0] = 1
for m in range(1, n+1):
for i in range(1, m+1):
a[m] += cnk(m, i) * a[m - i]
return a[n]
for i in range(1,10):
print(orderedbell(i))
1
3
13
75
541
4683
47293
545835
7087261

How to find a sum 3 elements in array?

I have an array A=[a1,a2,a3, ..., aN] I would like to take a product of each 3 elements:
s1=a1+a2+a3
s2=a4+a5+a6
...
sM=a(N-2)+a(N-1)+aN
My solution:
k=size(A);
s=0;
for n=1:k
s(n)=s(n-2)+s(n-1)+s(n);
end
Error: Attempted to access s(2); index out of bounds because numel(s)=1.
Hoe to fix it?
If you want to sum in blocks, for the general case when the number of elements of A is not necessarily a multiple of the block size, you can use accumarray:
A = [3 8 5 8 2 3 4 7 9 6 4]; % 11 elements
s = 3; % block size
result = accumarray(ceil((1:numel(A))/s).', A(:));
If you want a sliding sum with a given block size, you can use conv:
A = [3 8 5 8 2 3 4 7 9 6 4]; % 11 elements
s = 3; % block size
result = conv(A(:).', ones(1,s), 'valid');
You try to calculate sby using values from s. Dont you mean s(n)=A(n-2)+A(n-1)+A(n);? Also size returns more than one dimension on its own.
That being said, getting the 2 privous values n-2 and n-1 doenst work for n=1;2 (because you must have positive indices). You have to explain how the first two values should be handeled. I assume either 0 for elements not yet exisiting
k=size(A,2); %only the second dimension when A 1xn, or length(A)
s=zeros(1,k); %get empty values instead of appending each value for better performance
s(1)=A(1);
s(2)=A(2)+A(1);
for n=3:k %start at 3
s(n)=A(n-2)+A(n-1)+A(n);
end
or sshoult be 2 values shorter than A.
k=size(A,2);
s=zeros(1,k-2);
for n=1:k-2
s(n)=A(n)+A(n+1)+A(n+2);
end
You initialise s as a scalar with s = 0. Then you try and index it like an array, but it only has a single element.
Your current logic (if fixed) will calculate this:
s(1) = a(1)+a(2)+a(3)
s(2) = a(2)+a(3)+a(4)
...
% 's' will be 2 elements shorter than 'a'
So we need to be a bit wiser with the indexing to get what you describe, which is
s(1) = a(1)+a(2)+a(3)
s(2) = a(4)+a(5)+a(6)
...
% 's' will be a third as big as 'a'
You should pre-allocate s to the right size, like so:
k = numel(A); % Number of elements in 'A'
s = zeros( 1, k/3 ); % Output array, assuming 'k' is divisible by 3
for n = 0:3:k-3
s(n/3+1) = a(n+1) + a(n+2) + a(n+3);
end
You could do this in one line by reshaping the array to have 3 rows, then summing down each column, this assumes that the number of elements in a is divisible by 3, and that a is a row vector...
s = sum( reshape( a, 3, [] ) );

Inserting selected number of zeros between fixed number of non-zero elements in a vector in MATLAB

I have a vector like
A=[1,2,3,4,5,6,7,8,9,10];
I would like to insert 2 zero every 5 number.
The result would be A=[1,2,3,4,5,0,0,6,7,8,9,10,0,0].
I know I could preallocate the space and then use a for cycle to assign the variable, but I was wandering if there was some more elegant way.
This works even if A doesn't contain an integer number of blocks:
A = [1,2,3,4,5,6,7,8,9,10,11,12]; % input vector
m = 5; % block size
n = 2; % number of zeros to be added after each block
B = zeros(1, numel(A)+floor(numel(A)/m)*n); % preallocate to appropriate size
B(mod(0:end-1, m+n)<m) = A; % logical index. Fill values of A at desired positions of B
The result in this example is
B =
1 2 3 4 5 0 0 6 7 8 9 10 0 0 11 12
With A having number of elements a multiple of 5, you could use some reshaping and concatenation with zeros, like so -
reshape([reshape(A,5,[]) ; zeros(2,numel(A)/5)],1,[])

Matlab - elmination of rows in array that is not needed

I am comparing a 100 x 1 called "x" with the value from a 4 x 1 matrix called "xCP". I need a finalMatrix with only the values in x that are higher than the third value from xCP. However, the code i have below provides me with a 100 x 1 matrix with zeros in rows that the value are not higher. I only want the 25(e.g.) rows in finalMatrix that have the higher value. So my finalMatrix needs to be a 25 x 1 with values instead of 100 x 1 with 25 values and 75 zeros.
This is what i have so far:
K = size (x)
length = K(1)
finalMatrix = zeros(length,1);
count = 1;
for i=1:length;
if x(count,1) >= xCP(3)
finalMatrix(count,1) = x(count,1);
end
count =count+1;
end
Thank you!
You can add finalMatrix(count+1:length,1) = [] in the end of the code. It will remove the excess elements.

Matlab removing unwanted numbers from array

I have got a matlab script from net which generates even numbers from an inital value. this is the code.
n = [1 2 3 4 5 6];
iseven = [];
for i = 1: length(n);
if rem(n(i),2) == 0
iseven(i) = i;
else iseven(i) = 0;
end
end
iseven
and its results is this
iseven =
0 2 0 4 0 6
in the result i am getting both even numbers and zeros, is there any way i can remove the zeros and get a result like this
iseven =
2 4 6
To display only non-zero results, you can use nonzeros
iseven = [0 2 0 4 0 6]
nonzeros(iseven)
ans =
2 4 6
You can obtain such vector without the loop you have:
n(rem(n, 2)==0)
ans =
2 4 6
However, if you already have a vector with zeros and non-zeroz, uou can easily remove the zero entries using find:
iseven = iseven(find(iseven));
find is probably one of the most frequently used matlab functions. It returns the indices of non-zero entries in vectors and matrices:
% indices of non-zeros in the vector
idx = find(iseven);
You can use it for obtaining row/column indices for matrices if you use two output arguments:
% row/column indices of non-zero matrix entries
[i,j] = find(eye(10));
The code you downloaded seems to be a long-winded way of computing the range
2:2:length(n)
If you want to return only the even values in a vector called iseven try this expression:
iseven(rem(iseven,2)==0)
Finally, if you really want to remove 0s from an array, try this:
iseven = iseven(iseven~=0)
Add to the end of the vector whenever you find what you're looking for.
n = [1 2 3 4 5 6];
iseven = []; % has length 0
for i = 1: length(n);
if rem(n(i),2) == 0
iseven = [iseven i]; % append to the vector
end
end
iseven
To remove the all Zeros from the program we can use the following command,and the command is-
iseven(iseven==0)=[]

Resources