MATLAB busy, loop works well until i=29996, when i=29997 stay Busy - matlab

I am writing code which compares the data of a vector. It should count how many positions (acc) have equal values, and save a specific value in a vector of the same length of the quantity of positions (n_T(acc)).
My data vector is [30000 x 1]. For example, first 80 positions have the same value, next 60 positions have the same value, etc., next 5 positions have the same value.
The code works well if I use just 29996 values. I do not understand why when I try to use the complete vector MATLAB stays Busy.
Checking my data vector, I noticed that the last 5 positions are equivalent [29996:30000]. Could it be the reason, and what should I change?
Following is the code
%========================================================
%ac: data vector`
%acc1: accumulator which count how much positions have the same value
%n_T: vector which presents the values I need, in the same positions the data is equal
%m: show a value where i should begin
%========================================================
i=m; %previously used`
fv=length(ac)
while i<fv %29996
acc1=0;
for i=m+1:fv
if ac(i)==ac(i-1)
acc1=acc1+1; % count how much positions are equals
else
m=i;
break
end
end
mi=m-acc1; %define where the data n_T should begin
for i=mi:m
n_T(i)=tm/acc1; %create a vector with length [acc x1] begining in mi and finishing in m
end
m=i;
end
plot(n_T)

Does it work if you do this in a vectorized way? Not completely sure what you want the program to output.
% locate repeated elements
eq_els = ac(diff(ac) == 0);
% count number of repeated elements (unique)
unique_els = unique(eq_els);
num_equal_els = numel(unique_els);
% create variable-length lists for each unique element
each_eq_list = cell(num_equal_els,1);
for (k = 1:num_equal_els)
% each vector in the cell array is equal to the elements of ac that are equal to the current unique item
each_eq_list{[k]} = ac(ac == unique_els(k));
end
The length of each_eq_list{[k]} is the length of the total number of contiguous repeated values of repeated value k.

Related

MATLAB: how do I create a vector where each element is a function of the previous element

Working in Matlab.
I am trying to create a vector where each element is a function of the previous element. The goal is to put the first 50 (or so) values of a logistical function in a vector. So I start with 0.200, with r=4 (for example), the second element would then be 40.200(1-0.200)=0.640.
The third element would take the value 0.64 and perform the same function on that number, and so on...
I have tried a for-loop, but since there is no counter in the function, the loop doesn't work.
EDIT: I have created the following function:
n = 0;
x = 0.200;
for n=0:100
x=4*x*(1-x)
n=n+1
end
This gives the first 100 values. But I fail to get them as values in a vector...
Any suggestions on how to solve this would be appreciated.
You need to use indexing for the x vector you are creating. The way you currently have it coded, everything is going into a scalar x and not a vector x. E.g.,
n = 50; % the number of elements you want to fill
x = zeros(1,n); % allocate a vector to hold the numbers
x(1) = 0.200; % set the first value
for k=2:n % loop through the remaining values
x(k) = 4*x(k-1)*(1-x(k-1)); % set the next value using the previous value
end
Note that in the above code all of the usages of x other than the initial allocation involve indexing, e.g. x(1) and x(k) and x(k-1).

Performed a smoothing function on a matrix, but now only matching indices give values

I have a matrix 1000x1000x50 and I performed a function on each vector along the third dimension in a loop (1,000,000 vectors, 50 elements long). When I try to view any specific element where m=n, i.e. (1000,1000,40) , a nonzero value is displayed. However, when I try to view an element where m =/= n, i.e. (1000,1001,40), only a 0 is returned. I know that (1001,1001,40) has a nonzero value, and I know that the original matrix had a nonzero element at (1000,1001,40).
Here's the loop I used:
mymatrix_new = zeros(size(mymatrix));
for i=1:length(mymatrix)
mymatrix_new(i,i,:) = wdenoise(squeeze(mymatrix(i,i,:)));
end
For the values that DO display, the result is what I expected- a smoothed signal. I just don't understand why certain elements that are nonzero are displaying as zero when the m and n indices are't identical.
You are iterating and updating only the cells that have m == n. This happens because you use a single for loop. All other values are not visited and are never updated, this is why they remain zero.
If you look at your foor loop:
for i=1:length(mymatrix)
In the first iteration i = 1 and it will update:
mymatrix_new(1,1,:) = wdenoise(squeeze(mymatrix(1,1,:)));
In the second iteration i = 2 and it will update:
mymatrix_new(2,2,:) = wdenoise(squeeze(mymatrix(2,2,:)));
As you can see, you never update mymatrix_new(1, 2) or any cell other than the ones that have m == n == i
You need to use two nested for loops, such that you update all combinations of i and j
mymatrix_new = zeros(size(mymatrix));
for i=1:length(mymatrix)
for j=1:length(mymatrix) % Here it assumes the matrix is a square
mymatrix_new(i,j,:) = wdenoise(squeeze(mymatrix(i,j,:)));
end
end

Mean value of each column of a matrix

I have a 64 X 64 matrix that I need to find the column-wise mean values for.
However, instead of dividing by the total number of elements in each column (i.e. 64), I need to divide by the total number of non-zeros in the matrix.
I managed to get it to work for a single column as shown below. For reference, the function that generates my matrix is titled fmu2(i,j).
q = 0;
for i = 1:64
if fmu2(i,1) ~= 0;
q = q + 1;
end
end
for i = 1:64
mv = (1/q).*sum(fmu2(i,1));
end
This works for generating the "mean" value of the first column. However, I'm having trouble looping this procedure so that I will get the mean for each column. I tried doing a nested for loop, but it just calculated the mean for the entire 64 X 64 matrix instead of one column at a time. Here's what I tried:
q = 0;
for i = 1:64
for j = 1:64
if fmu2(i,j) ~= 0;
q = q +1;
end
end
end
for i = 1:64
for j = 1:64
mv = (1/q).*sum(fmu2(i,j));
end
end
Like I said, this just gave me one value for the entire matrix instead of 64 individual "means" for each column. Any help would be appreciated.
For one thing, do not call the function that generates your matrix in each iteration of a loop. This is extremely inefficient and will cause major problems if your function is complex enough to have side effects. Store the return value in a variable once, and refer to that variable from then on.
Secondly, you do not need any loops here at all. The total number of nonzeros is given by the nnz function (short for number of non-zeros). The sum function accepts an optional dimension argument, so you can just tell it to sum along the columns instead of along the rows or the whole matrix.
m = fmu2(i,1)
averages = sum(m, 1) / nnz(m)
averages will be a 64-element array with an average for each column, since sum(m, 1) is a 64 element sum along each column and nnz(m) is a scalar.
One of the great things about MATLAB is that it provides vectorized implementations of just about everything. If you do it right, you should almost never have to use an explicit loop to do any mathematical operations at all.
If you want the column-wise mean of non-zero elements you can do the following
m = randi([0,5], 5, 5); % some data
avg = sum(m,1) ./ sum(m~=0,1);
This is a column-wise sum of values, divided by the column-wise number of elements not equal to 0. The result is a row vector where each element is the average of the corresponding column in m.
Note this is very flexible, you could use any condition in place of ~=0.

Matlab's trick to increment variable

How to increment a variable by a infinite set of numbers, in Matlab. I'v a variable which I want to increment till the loop ends by 0.1 every time but through set of range.
I'm currently doing this by: K=K*0.1; %K = 2 initially but I want this same by Matlab's trick of ranged values like [0.1:0.1:9] where 9 is the loop condination.
My Code:
K=2;
for ii=1:9
K=K*0.1;
end
If I understand correctly:
for K = 2 * 0.1.^(1:9)
%// do something with K
end
You can try using the cumprod command which returns the cumulative product of the elements in a matrix or vector. For your example, something like:
K=cumprod([2 repmat(0.1,1,9)]); % returns a row vector of 9 elements
repmat just creates a row vector of nine elements each set to the value 0.1. The last element in the vector, K(end), will be the product returned by your example. i.e.K = 2*0.1^9;

How can I generate a binary matrix with specific patterns?

I have a binary matrix of size m-by-n. Given below is a sample binary matrix (the real matrix is much larger):
1010001
1011011
1111000
0100100
Given p = m*n, I have 2^p possible matrix configurations. I would like to get some patterns which satisfy certain rules. For example:
I want not less than k cells in the jth column as zero
I want the sum of cell values of the ith row greater than a given number Ai
I want at least g cells in a column continuously as one
etc....
How can I get such patterns satisfying these constraints strictly without sequentially checking all the 2^p combinations?
In my case, p can be a number like 2400, giving approximately 2.96476e+722 possible combinations.
Instead of iterating over all 2^p combinations, one way you could generate such binary matrices is by performing repeated row- and column-wise operations based on the given constraints you have. As an example, I'll post some code that will generate a matrix based on the three constraints you have listed above:
A minimum number of zeroes per column
A minimum sum for each row
A minimum sequential length of ones per column
Initializations:
First start by initializing a few parameters:
nRows = 10; % Row size of matrix
nColumns = 10; % Column size of matrix
minZeroes = 5; % Constraint 1 (for columns)
minRowSum = 5; % Constraint 2 (for rows)
minLengthOnes = 3; % Constraint 3 (for columns)
Helper functions:
Next, create a couple of functions for generating column vectors that match constraints 1 and 3 from above:
function vector = make_column
vector = [false(minZeroes,1); true(nRows-minZeroes,1)]; % Create vector
[vector,maxLength] = randomize_column(vector); % Randomize order
while maxLength < minLengthOnes, % Loop while constraint 3 is not met
[vector,maxLength] = randomize_column(vector); % Randomize order
end
end
function [vector,maxLength] = randomize_column(vector)
vector = vector(randperm(nRows)); % Randomize order
edges = diff([false; vector; false]); % Find rising and falling edges
maxLength = max(find(edges == -1)-find(edges == 1)); % Find longest
% sequence of ones
end
The function make_column will first create a logical column vector with the minimum number of 0 elements and the remaining elements set to 1 (using the functions TRUE and FALSE). This vector will undergo random reordering of its elements until it contains a sequence of ones greater than or equal to the desired minimum length of ones. This is done using the randomize_column function. The vector is randomly reordered using the RANDPERM function to generate a random index order. The edges where the sequence switches between 0 and 1 are detected using the DIFF function. The indices of the edges are then used to find the length of the longest sequence of ones (using FIND and MAX).
Generate matrix columns:
With the above two functions we can now generate an initial binary matrix that will at least satisfy constraints 1 and 3:
binMat = false(nRows,nColumns); % Initialize matrix
for iColumn = 1:nColumns,
binMat(:,iColumn) = make_column; % Create each column
end
Satisfy the row sum constraint:
Of course, now we have to ensure that constraint 2 is satisfied. We can sum across each row using the SUM function:
rowSum = sum(binMat,2);
If any elements of rowSum are less than the minimum row sum we want, we will have to adjust some column values to compensate. There are a number of different ways you could go about modifying column values. I'll give one example here:
while any(rowSum < minRowSum), % Loop while constraint 2 is not met
[minValue,rowIndex] = min(rowSum); % Find row with lowest sum
zeroIndex = find(~binMat(rowIndex,:)); % Find zeroes in that row
randIndex = round(1+rand.*(numel(zeroIndex)-1));
columnIndex = zeroIndex(randIndex); % Choose a zero at random
column = binMat(:,columnIndex);
while ~column(rowIndex), % Loop until zero changes to one
column = make_column; % Make new column vector
end
binMat(:,columnIndex) = column; % Update binary matrix
rowSum = sum(binMat,2); % Update row sum vector
end
This code will loop until all the row sums are greater than or equal to the minimum sum we want. First, the index of the row with the smallest sum (rowIndex) is found using MIN. Next, the indices of the zeroes in that row are found and one of them is randomly chosen as the index of a column to modify (columnIndex). Using make_column, a new column vector is continuously generated until the 0 in the given row becomes a 1. That column in the binary matrix is then updated and the new row sum is computed.
Summary:
For a relatively small 10-by-10 binary matrix, and the given constraints, the above code usually completes in no more than a few seconds. With more constraints, things will of course get more complicated. Depending on how you choose your constraints, there may be no possible solution (for example, setting minRowSum to 6 will cause the above code to never converge to a solution).
Hopefully this will give you a starting point to begin generating the sorts of matrices you want using vectorized operations.
If you have enough constraints, exploring all possible matrices could be attempted:
// Explore all possibilities starting at POSITION (0..P-1)
explore(int position)
{
// Check if one or more constraints can't be verified anymore with
// all values currently set.
invalid = ...;
if (invalid) return;
// Do we have a solution?
if (position >= p)
{
// print the matrix
return;
}
// Set one more value and continue exploring
for (int value=0;value<2;value++)
{ matrix[position] = value; explore(position+1); }
}
If the number of constraints is low, this approach will take too much time.
In this case, for the kind of constraints you gave as examples, simulated annealing may be a good solution.
You must design an energy function, high when all constraints are met. That would be something like that:
Generate a random matrix
Compute energy E0
Change one cell
Compute energy E1
If E1>E0, or E0-E1 is smaller than f(temperature), keep it, otherwise reverse the move
Update temperature, and goto 2 unless stop criterion is reached
If all the contraints relate to columns (as is the case in the question), then you can find all possible valid columns and check that each column in the matrix is in this set. (i.e. when you consider each column independently, you reduce the number of possibilities a lot.)
I might be way off here, but I remember doing something similar once with some genetic algorithm.
Check out pseudo boolean constraints (also called 0-1 integer programming).
This is virtually impossible if your constraint set is complex enough. You might try to use a stochastic optimizer, like simulated annealing, particle swarm optimization, or a genetic algorithm to find a feasible solution.
However, if you can generate one (non-random) solution to such a problem, then often you can generate others by random permutations made to the existing solution.