Matlab - elmination of rows in array that is not needed - arrays

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.

Related

Hackerrank array manipulation assistance

This is related to the question "Manipulate Arrays" on hackerrank: https://www.hackerrank.com/challenges/crush/problem
Could you explain why this code subtracts from the 2nd element in the queries array and how is it totalling the numbers at the end. I added print statements to see why this works but I am stuck.
def arrayManipulation(n, queries):
arr = [0]*n
for i in queries:
arr[i[0] - 1] += i[2]
if i[1] != len(arr):
arr[i[1]] -= i[2]
maxval = 0
itt = 0
for q in arr:
itt += q
if itt > maxval:
maxval = itt
return maxval
Instead of adding the value to each element in the range, the value is added to the first element of the range and subtracted from the element after the last element of the range. This way, when iterating over the array from the beginning and summing all the values, you get the current value of each element. So, an example with n == 5:
0 0 0 0 0
query 1 3 100
100 0 0 -100 0
query 2 4 200
100 200 0 -100 -200
If you now iterate over the array and sum the values while doing so, you will get the values:
100 300 300 200 0
which is the correct state of the array after such queries.
Edit: For queries where the ending index is equal to the length of the array the value is not subtracted from anything because there are no elements after the last one, so there is no point in doing so.

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

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

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,[])

Create N x 2 array from N x 1 array-Matlab

I have a 1D array (say A) of size N (i.e N x 1; N-rows, 1 Column). Now I want to create an array of size N x 2 (N-rows, 2-columns) with the array A as one column and the other column with a same element (0 in the given example below).
For e.g If
A =[1;2;3;4;5];
I'd like to create a matrix B which is
B=[0 1; 0 2; 0 3; 0 4; 0 5]
How do I do this in Matlab?
You can also abuse bsxfun for a one-liner -
bsxfun(#times,[0,1],A)
Or matrix-multiplication for that implicit expansion -
A*[0,1]
You can initialize B to be an Nx2 array of all zeros and then assign the second column to the values in A.
A = [1;2;3;4;5];
B = zeros(numel(A), 2);
B(:,2) = A;
% 0 1
% 0 2
% 0 3
% 0 4
% 0 5
If you actually just want zeros in that first column, you don't even have to initialize B as MATLAB will automatically fill in the unknown values with 0.
% Make sure B isn't already assigned to something
clear B
% Assign the second column of uninitialized variable B to be equal to A
B(:,2) = A;
You can try this approach
B=[zeros(length(A),1) A]

How to check if all the entries in columns of a matrix are equal (in MATLAB)?

I have a matrix of growing length for example a 4-by-x matrix A where x is increasing in a loop. I want to find the smallest column c where all columns before that, each, carry one single number. The matrix A can look like:
A = [1 2 3 4;
1 2 3 5;
1 2 3 1;
1 2 3 0];
where c=3, and x=4.
At each iteration of the loop where A grows in length, the value of index c grows as well. Therefore, at each iteration, I want to update the value of c. How efficiently can I code this in Matlab?
Let's say you had the matrix A and you wanted to check a particular column iito see if all its elements are the same. The code would be:
all(A(:, ii)==A(1, ii)) % checks if all elements in column are same as first element
Also, keep in mind that once the condition is broken, x cannot be updated anymore. Therefore, your code should be:
x = 0;
while true
%% expand A by one column
if ~all(A(:, x)==A(1, x)) % true if all elements in column are not the same as first element
break;
end
x = x+1;
end
You could use this:
c = find(arrayfun(#(ind)all(A(1, ind)==A(:, ind)), 1:x), 1, 'first');
This finds the first column where not all values are the same. If you run this in a loop, you can detect when entries in a column start to differ:
for x = 1:maxX
% grow A
c = find(arrayfun(#(ind)~all(A(1, ind)==A(:, ind)), 1:x), 1, 'first');
% If c is empty, all columns have values equal to first row.
% Otherwise, we have to subtract 1 to get the number of columns with equal values
if isempty(c)
c = x;
else
c = c - 1;
end
end
Let me give a try as well:
% Find the columns which's elements are same and sum the logical array up
c = sum(A(1,:) == power(prod(A,1), 1/size(A,1)))
d=size(A,2)
To find the last column such that each column up to that one consists of equal values:
c = find(any(diff(A,1,1),1),1)-1;
or
c = find(any(bsxfun(#ne, A, A(1,:)),1),1)-1;
For example:
>> A = [1 2 3 4 5 6;
1 2 3 5 5 7;
1 2 3 1 5 0;
1 2 3 0 5 8];
>> c = find(any(diff(A,1,1),1),1)-1
c =
3
You can try this (easy and fast):
Equal_test = A(1,:)==A(2,:)& A(2,:)==A(3,:)&A(3,:)==A(4,:);
c=find(Equal_test==false,1,'first')-1;
You can also check the result of find if you want.

Resources