My question may be a bit "silly", but I am stucked here, so I would really need your help.
So, we have a cell array Q 5520x1, like the one below:
I'm only interested in the first two numbers of each row. To be specifically clear, giving
K>> Q{1}
ans = 0 1 0 238
I only care about 0 and 1, and so on with the other rows. Only the first two numbers of each row. Let's just ignore the rest values in each row of Q, they are more or less irrelevant.
My problem is, how is it possible to check if the first value of each row in the cell array Q, in the example above 0, remains the same? What I would like to achieve, is while the first value is the same, I want to store in another cell array, all the second values. In case of number 0, I want to store the values
[1,3,127,129,216,217,252,253,302,303,342,343]
and so on. How could this be done?
Any help or advice would be really appreciated.
% Example Q
Q = {[0 1 135 134];
[0 3 154 26];
[0 16 146 234];
[1 2 324 125];
[1 7 213 252]};
A = cell2mat(Q(1:2760));
[~,~,ind2] = unique(A(:,1));
C = arrayfun(#(x)A(find(ind2==x),2),1:max(ind2),'UniformOutput',false);
C{1}
ans =
1
3
16
C{2}
ans =
2
7
You can use unique and accumarray as follows:
Q = {[0 1 0 238]
[0 3 1 84]
[1 2 1 85]
[3 4 5 6]}; %// example data. Cell array of equal-size row vectors
Qm = vertcat(Q{:}); %// convert Q into matrix
[vals, ~, id] = unique(Qm(:,1)); %// get unique id for 1st col
result = accumarray(id, Qm(:,2), [], #(x){x.'}); %'// gather 2nd col values according to id
This gives:
vals =
0
1
3
result{1} =
1 3
result{2} =
2
result{3} =
4
You can do it with a for loop :
cell_range = max(cellfun(#(x) x(1),Q));
C = cell(cell_range,1)
for i = 1:length(Q)
M = Q{i};
disp(M(1));
C{M(1)} = [C{M(1)}, M(2)];
end
Related
I would like to eliminate all the columns in which the third row contain zero values in my dataset.
As an example:
original_data = [1 2 3 4 5; 1 2 3 4 5; 0 0 0 1 2]
For the first three columns (with zeros on third line), I would like to create a new array in which the colums with zeros in third line are deleted to get the result:
new_data = [ 4 5; 4 5; 1 2]
I would also like an array of the column indices of the non-zero values in the original array.
For example:
original_indices = [4, 5]
I tried:
dados_teste = dados_out_15;
dados_p6 = [];
[m,n] = size(dados_teste)
for i = 1:n
if dados_teste(3:i) == 0;
dados_p6 = dados_teste(:,i)
else
dados_p6 = dados_teste(:,n)
end
end
But it clearly does not work...
I would apply the find() function to find all the non-zero indices, then apply matrix indexing to generate a new array that only contains the columns corresponding to the non-zero indices in the third row.
Sample_Array = [20 30 40 50; 30 20 70 90; 0 2 1 2];
%Grabbing the third row of the matrix%
Third_Row = Sample_Array(3,:);
%Finding all the non-zero indices%
[Non_Zero_Indices] = find(Third_Row);
%Using matrix indices to generate a new array based on the non-zero
%indicies%
New_Matrix = Sample_Array(:,Non_Zero_Indices);
%Printing matrices%
Sample_Array
New_Matrix
Non_Zero_Indices
I am trying to allocate (x, y) points to the cells of a non-uniform rectangular grid. Simply speaking, I have a grid defined as a sorted non-equidistant array
xGrid = [x1, x2, x3, x4];
and an array of numbers x lying between x1 and x4. For each x, I want to find its position in xGrid, i.e. such i that
xGrid(i) <= xi <= xGrid(i+1)
Is there a better (faster/simpler) way to do it than arrayfun(#(x) find(xGrid <= x, 1, 'last'), x)?
You are looking for the second output of histc:
[~,where] = histc(x, xGrid)
This returns the array where such that xGrid(where(i)) <= x(i) < xGrid(where(i)+1) holds.
Example:
xGrid = [2,4,6,8,10];
x = [3,5,6,9,11];
[~,where] = histc(x, xGrid)
Yields the following output:
where =
1 2 3 4 0
If you want xGrid(where(i)) < x(i) <= xGrid(where(i)+1), you need to do some trickery of negating the values:
[~,where] = histc(-x,-flip(xGrid));
where(where~=0) = numel(xGrid)-where(where~=0)
This yields:
where =
1 2 2 4 0
Because x(3)==6 is now counted for the second interval (4,6] instead of [6,8) as before.
Using bsxfun for the comparisons and exploiting find-like capabilities of max's second output:
xGrid = [2 4 6 8]; %// example data
x = [3 6 5.5 10 -10]; %// example data
comp = bsxfun(#gt, xGrid(:), x(:).'); %'// see if "x" > "xGrid"
[~, result] = max(comp, [], 1); %// index of first "xGrid" that exceeds each "x"
result = result-1; %// subtract 1 to find the last "xGrid" that is <= "x"
This approach gives 0 for values of x that lie outside xGrid. With the above example values,
result =
1 3 2 0 0
See if this works for you -
matches = bsxfun(#le,xGrid(1:end-1),x(:)) & bsxfun(#ge,xGrid(2:end),x(:))
[valid,pos] = max(cumsum(matches,2),[],2)
pos = pos.*(valid~=0)
Sample run -
xGrid =
5 2 1 6 8 9 2 1 6
x =
3 7 14
pos =
8
4
0
Explanation on the sample run -
First element of x, 3 occurs last between ...1 6 with the criteria of xGrid(i) <= xi <= xGrid(i+1) at the backend of xGrid and that 1 is at the eight position, so the first element of the output pos is 8. This continues for the second element 7, which is found between 6 and 8 and that 6 is at the fourth place in xGrid, so the second element of the output is 4. For the third element 14 which doesn't find any neighbours to satisfy the criteria xGrid(i) <= xi <= xGrid(i+1) and is therefore outputted as 0.
If x is a column this might help
xg1=meshgrid(xGrid,1:length(x));
xg2=ndgrid(x,1:length(xGrid));
[~,I]=min(floor(abs(xg1-xg2)),[],2);
or a single line implementation
[~,I]=min(floor(abs(meshgrid(xGrid,1:length(x))-ndgrid(x,1:length(xGrid)))),[],2);
Example: xGrid=[1 2 3 4 5], x=[2.5; 1.3; 1.7; 4.8; 3.3]
Result:
I =
2
1
1
4
3
Given a vector
X = [1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3]
I would like to generate a vector such
Y = [1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5]
So far what I have got is
idx = find(diff(X))
Y = [1:idx(1) 1:idx(2)-idx(1) 1:length(X)-idx(2)]
But I was wondering if there is a more elegant(robust) solution?
One approach with diff, find & cumsum for a generic case -
%// Initialize array of 1s with the same size as input array and an
%// intention of using cumsum on it after placing "appropriate" values
%// at "strategic" places for getting the final output.
out = ones(size(X))
%// Find starting indices of each "group", except the first group, and
%// by group here we mean run of identical numbers.
idx = find(diff(X))+1
%// Place differentiated and subtracted values of indices at starting locations
out(idx) = 1-diff([1 idx])
%// Perform cumulative summation for the final output
Y = cumsum(out)
Sample run -
X =
1 1 1 1 2 2 3 3 3 3 3 4 4 5
Y =
1 2 3 4 1 2 1 2 3 4 5 1 2 1
Just for fun, but customary bsxfun based alternative solution -
%// Logical mask with each column of ones for presence of each group elements
mask = bsxfun(#eq,X(:),unique(X(:).')) %//'
%// Cumulative summation along columns and use masked values for final output
vals = cumsum(mask,1)
Y = vals(mask)
Here's another approach:
Y = sum(triu(bsxfun(#eq, X, X.')), 1);
This works as follows:
Compare each element with all others (bsxfun(...)).
Keep only comparisons with current or previous elements (triu(...)).
Count, for each element, how many comparisons are true (sum(..., 1)); that is, how many elements, up to and including the current one, are equal to the current one.
Another method is using the function unique
like this:
[unqX ind Xout] = unique(X)
Y = [ind(1):ind(2) 1:ind(3)-ind(2) 1:length(X)-ind(3)]
Whether this is more elegant is up to you.
A more robust method will be:
[unqX ind Xout] = unique(X)
for ii = 1:length(unqX)-1
Y(ind(ii):ind(ii+1)-1) = 1:(ind(ii+1)-ind(ii));
end
Please help me solve this problem...
a = [1 2 3 4 5 6 7 8 9 10]
b = [12 4 13 7 5 7 8 10 3 12]
c = [4 5 3 2 6 7 5 3 4 5]
I have to find the first value on a, if the value on b is less than 10 for more than 3 consecutive places and index for the starting of satisfying the condition. Also the value of c after finding the value of b for same index.
Ans should be index for b=4, index for a=4 and value for a =4 and c=2
Thank you in advance
You may use strfind as one approach -
str1 = num2str(b <10,'%1d') %%// String of binary numbers
indx = strfind(['0' str1],'0111') %%// Indices where the condition is met
ind = indx(1) %%// Choose the first occurance
a_out = a(ind) %%// Index into a
c_out = c(ind) %%// Index into c
Output -
ind =
4
a_out =
4
c_out =
2
To find a given number of consecutive values lower than a threshold, you can apply conv to a vector of 0-1 values resulting from the comparison:
threshold = 10; %// values "should" be smaller than this
number = 4; %// at least 4 consecutive occurrences
ind = find(conv(double(b<threshold), ones(1,number), 'valid')==number, 1);
%// double(b<threshold) gives 0-1.
%// conv(...)==... gives 1 when the sought number of consecutive 1's is reached
%// find(... ,1) gives the first index where that happens
a_out = a(ind);
c_out = c(ind);
I have matrix nx3 like this
A = [ 1 3 50;
1 4 80;
1 6 75;
2 3 20;
3 6 10;
6 8 20;
6 9 99;
. . .
. . .
]
I want to check the first index that have same
=> check the third element and pick the maximum value and re arrange matrix
it should be like
Ans = [1 4 80;
2 3 20;
6 9 99;
. . .
]
I was thinking use max() check to on the third element but how can I detect the first element on matrix that are repeated
To produce the same results as Luis Mendo
Ans = sortrows(A, 3);
[~, J] = unique(Ans(:,1));
Ans = Ans(J,:);
%// Obtain unique values of col 1. Each value will determine a group of rows:
ii = unique(A(:,1));
%// For each group of rows, compute maximum of column 3. This is done efficiently
%// with accumarray. Use its sparse option to avoid memory problems , in case
%// values of column 1 are very disperse:
kk = nonzeros(accumarray(A(:,1),A(:,3),[],#max,[],true));
%// Select indices of rows whose column 3 contains the maximum of the group
%// determined by column 1. This is done efficiently using bsxfun twice:
m = any(bsxfun(#eq, A(:,1).', ii) & bsxfun(#eq, A(:,3).', kk));
%// Build result:
result = A(m,:);
In your example:
result =
1 4 80
2 3 20
3 6 10
6 9 99