Find the intersection of a vector and cell array in MATLAB - matlab

I have a vector, let say as=[1 3 4] and I have 30 by 30 cell array. I want to check if the elements of vector as intersect with the elements of each cell or not? If so, I want to find the indices of the cell.

Assuming cellarr to be input cell array, see if this approach works for you -
%// Find intersecting elements for each cell
int_idx = cellfun(#(x) intersect(x,as),cellarr,'UniformOutput', false)
%// Find non empty cells that denote intersecting cells.
%// Then, find their row and column indices
[row_ind,col_ind] = find(~cellfun('isempty',int_idx))
Another approach with ismember to find matches among each cell and if there is any match within a cell, find the indices of it -
[row_ind,col_ind] =find(cell2mat(cellfun(#(x) any(ismember(x,as)),cellarr,'un', 0)))
And another -
%// Vertically concatenate all numeric array from cells
vertcat_cells = vertcat(cellarr{:})
%// Get all good matches
matches = any(any(bsxfun(#eq,vertcat_cells,permute(as,[1 3 2])),2),3)
%// Reshape matches into the size of cellarr and get indices of matches
[row_ind,col_ind] = find(reshape(matches,size(cellarr)))

Related

How to get the value of each cell in a cell array

I wanna ask a question about cell array. Suppose I have a cell array
C={[2 1], [3 5], [15 6]};
I'd like to get all first value of each cell, which are [2, 3 , 15]
however, when I type
C{:}(1);
or
C(:)(1);
There would be some mistakes.
So what is the simplist way to get those numbers.
If every cell is a vector and has the same amount of elements, one way is to create a matrix by stacking all of these cells together and extract out the first column. Use vertcat to help you do that:
CMat = vertcat(C{:});
out = CMat(:,1);
If every cell does not have the same amount of elements, one way is to use cellfun. Use an anonymous function to extract out the first element over each cell array:
out = cellfun(#(x) x(1), C);
The benefit of the above approach is that it doesn't matter if each cell is a vector or matrix. It'll extract the first element for a vector or the top-left corner for a matrix.

Adding 0's to cell array such that each column contains an equal number of entries - MATLAB

I have a 16x100 (varies in size) cell array and I would like to extract each of it's columns into a column of a matrix. When each column of the cell array contains an identical number of entries I can use:
elem = numel([dist{:,1}]);
repeat = size(dist,2);
data = zeros(elem,repeat);
for k=1:repeat
results(:,k) = [dist{:,k}]';
end
However there are some instances where there are not an equal number thus it returns the error:
Subscripted assignment dimension mismatch.
What is the best way around this? Is there a way to add zeroes to equalise the number of entries?
Perfect setup for bsxfun's masking capability here!
Now, I am assuming your data is setup as described in your previous question -
To solve the case of filling up "empty spaces" with zeros, you can setup an output array with maximum possible number of elements in each column and then fillup the valid spaces with the values from the input cell array, with the valid spaces being detected by the logical mask created with bsxfun. Read on through the comments inlined within the code listed next to find out the exact ideas on solving it -
%// Get the number of elements in each column of the input cell array
lens = sum(cellfun('length',a),1)
%// Store the maximum number of elements possible in any column of output array
max_lens = max(lens)
%// Setup output array, with no. of rows as max number of elements in each column
%// and no. of columns would be same as the no. of columns in input cell array
results = zeros(max_lens,numel(lens))
%// Create as mask that has ones to the "extent" of number of elements in
%// each column of the input cell array using the lengths
mask = bsxfun(#le,[1:max_lens]',lens) %//'
%// Finally, store the values from input cell array into masked positions
results(mask) = [a{:}]

How to compare two cell elements in matlab?

I am using two cells for storing the targeted and the expected value of a neural network process in matlab. I have used two 1*1 cell array for storing the values respectively. And here is my code.
cinfo=cell(1,2)
cinfo(1,1)=iter(1,10)%value is retrieved from a dataset iter
cinfo(1,2)=iter(1,11)
amp1=cinfo(1,1);
amp2=cinfo(1,2);
if amp1 == amp2
message=sprintf('NOT DETECTED BY THE DISEASE');
uiwait(msgbox(message));
But when i run the above code, the get the following error :
??? Undefined function or method 'eq' for input arguments of type 'cell'.
Error in ==> comparison at line 38
if amp1 == amp2
How to solve this problem?
The problem is how you indexed things. A 1x1 cell array does not make a lot of sense, instead get the actual element in the single cell, by indexing with curly brackets:
amp1=cinfo{1,1}; # get the actual element from the cell array, and not just a
amp2=cinfo{1,2}; # 1x1 cell array by indexing with {}
if (amp1 == amp2)
## etc...
However, note that if amp1 and amp2 are not scalars the above will act weird. Instead, do
if (all (amp1 == amp2))
## etc...
Use isequal. That will work even if the cell's contents have different sizes.
Example:
cinfo=cell(1,2);
cinfo(1,1) = {1:10}; %// store vector of 10 numbers in cell 1
cinfo(1,2) = {1:20}; %// store vector of 20 numbers in cell 2
amp1 = cinfo(1,1); %// single cell containing a length-10 numeric vector
amp2 = cinfo(1,2); %// single cell containing a length-20 numeric vector
if isequal(amp1,amp2)
%// ...
In this example, which parallels your code, amp1 and amp2 are cell arrays consisting of a single cell which contains a numeric vector. Another possibility is to directly store each cell's contents into amp1, amp2, and then compare them:
amp1 = cinfo{1,1}; %// length-10 numeric vector
amp2 = cinfo{1,2}; %// length-20 numeric vector
if isequal(amp1,amp2)
%// ...
Note that even in this case, the comparisons amp1==amp1 or all(amp1==amp2) would give an error, because the vectors have different sizes.

how to insert elements of matrix into cell array at indices specified by another column of the matrix

I have a matrix A which is a n X 2 matrix of floats with the second column in each row representing the column index of the value in the first column. I would ideally like to vectorize the insertion of elements in the first column of A in the row rowIndex and their respective columns as specified by A(:,2).
The pseudo-code for what I am looking to achieve is as follows:
myCellArray = cell(n X n)
%rowIndex is some predefined integer.
rowIndex
%A is my n X 2 matrix of values and corresponding column indices.
A
myCellArray{(rowIndex*ones(size(A(:,1),1),1)),A(:,2)} = A(:,1)
I have provided code for what I have tried at the bottom although I have tried something similar to the last line and it has failed hence I was wondering how something like this would work in MATLAB. Basically if my question is confusing, I am looking to vectorize insertion of elements into cell array by using a vector of indices and vector of values to insert at each index.
a{1,z(:,2)} = z(:,1)
Here I am trying to insert all values in the first column of z matrix into the cells indexed by 1 and the corresponding entry in the second column of z into the cell array.
Use
a(1,z(:,2)) = mat2cell(z(:,1), ones(1,size(z,1)), 1);
For example, with
z = [1 2
3 4];
this results in
a =
[] [1] [] [3]

Reshaping a cell array in matlab

Hi I have a cell array which is called vector, with dimensions 69083x2 , now i want to reshape this cell array to 3212762x2, but reshape(vector,3212762,2) does not work. I get this error:
To RESHAPE the number of
elements must not change.
Can anyone tell me how I can do this ?
Do you mean you wish to make the cell array larger? reshape is to store the same elements in a different 'shape', for eg., a 3x2 cell array as a 6x1 cell array - note that the total number of elements remains 6 in both cases.
If you wish to enlarge the cell array, just assign something to the last element of the enlarged cell array like so:
vector(3212762, 2) = {[]}
Now vector would be of size 3212762x2.
Just like sundar mentioned
vector(3212762, 2) = 0
will give you 3212762x2 matrix with the new rows assigned to 0.