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.
Related
I have a 1x24 cell array called chaining, whose each cell contains a 119x119 matrix:
I want to find the sum of each corresponding elements of all the cells, and store them in a matrix called result. That is, the (j,k)th element of result should contains the sum of the (j,k)th elements of all the matrices in the cell array chaining.
The code I wrote to do this is:
for j=1:size(chaining,2)
for k=1:size(chaining,2)
result(j,k) = sum(chaining{1,:}(j,k));
end
end
But this gives error because apparently MATLAB can't aggregate cell arrays for some reason (i.e., the chaining{1,:} part).
Could anyone please show me how to go about doing this?
how about
result = sum( cat(3, chaining{:}), 3 );
What just happened here?
First, we convert the cell array into a 3D array by "stacking" the 2D cell elements on the third dimension:
cat(3, chaining{:})
Once we have a 3D array of size 119-by-119-by-24 we can sum along the third dimension and get result of size 119-by-119:
sum( ..., 3 );
For more info see cat and sum help pages.
BTW,
If you insist on chaining{1,:}(jj,kk) type of solution (not recommended), you might find subsref command useful.
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.
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)))
I have a matrix and I want to put into the third column of the matrix, elements from a cell array. How can I do this?
Here is an example of what I mean.
This is the matrix (E):
43.4350000000000 -88.5277780000000 NaN 733144
43.4350000000000 -88.5277780000000 NaN 733146
43.4350000000000 -88.5277780000000 NaN 733148
43.4350000000000 -88.5277780000000 NaN 733150
I want to take the NaN column (column 3) and put into it, the elements of a cell array (uID)
The cell array looks like this:
'027-0007'
'079-0026'
'119-8001'
'133-0027'
I used this code:
E(:,3) = reshape(repmat(uID',length(all_dates),1),[],1)
to replicate each line of uID a certain number of times and then reshape it into a column so that's it's the same size as a column of E.
However, when I run it now, the fact that E is a matrix and uID is a cell causes MATLAB to tell me thatConversion to double from cell is not possible. The part to the right of the = works fine. It's the placing the cell elements into E that's causing the problem.
Instead of inserting the data into a normal matrix, you can insert it into another cell
Ecell=num2cell(E);
Ecell(:,3)=uID;
The contents of your cell array are not numeric and therefore cannot be inserted into a numeric matrix. You can use str2double to convert strings cell arrays to numeric arrays like in the following
>> str2double({'3','17.5'})
ans =
3.0000 17.5000
but that's only when the string contents of the cell represent actual numbers, which doesn't seem to be true in your case.
and have two vectors - a and b. a is of class double, and b is of class cell. I want to create a 2 x length(a) cell array that pairs the 1st value of a with the second value of b and so on....
I have so far
for i=1:length(a)
for j=1:length(ab
c{j,i} = {a(j),cell2mat(b(i))};
end
end
where each output of my new structure is something like this is c{1,1}:
c{1,1} =
[-0.1065] [1x499 char]
where I cannot seem to access the second element.My question is is there a way to access that second element for each row in the cell array, or have I done this wrong?
Thanks very much.
No need for a loop. This is how you can do it assuming both your cell and numeric vectors are columns:
a=[1:4]';
b={'a';'b';'c';'d'};
c=[num2cell(a),b] % combine a to b in a cell array
You are creating a very strange datastructure, cell of cell of arrays.
x=c{1,1}
first=x{1}
second=x{2}