I have a cell array of cell arrays...
data = {1x16}
{1x16}
{1x16}
and I am trying to retrieve the first value from each of the cells.. However when I do
data{:}(1)
I end up with an error saying
Bad cell reference operation.
First of all is there a good reason as to why I shouldn't be allowed to do this? And secondly is there a way around this?
Cheers!
Ben
For a 1D cell array -
first_vals = arrayfun(#(n) data{n}(1),1:numel(data))
This should work for a general case -
first_vals = reshape(arrayfun(#(n) data{n}(1),1:numel(data)),size(data))
I would prefer doing it using a simple for loop :
For instance, this would work :
data = [{[1:16]}; {[17:32]}; {[33:48]}];
b= []
for i=1:length(data)
b = [b data{i,1}(1)];
end
Related
I have an array and a cell array with the same dimensions: A is a 1x2492 double array, and B is an 1x2492 cell cell array. I want to make a new cell array that assigns the values in A to the corresponding column values of B. Here was my code:
for n = 1:numel(B)
newArray(n) = [A(n),B{n}(2)];
newCellArray{n} = newArray;
end
When I ran the code, I got the error 'Subscripted assignment dimension mismatch.'
I think it's because some cells in B have multiple columns, and the code loop doesn't recognize that I want to assign the same value of A to all values in the cell.
For example, if cell 1 of B contains:
2 2355
23 1293
37 1222
I would like my code loop to assign the corresponding first value of A to 2355, 1293, and 1222. So basically, I'd like to have a new cell like this:
1 2355
1 1293
1 1222
I realize that this is a very confusing explanation, but I hope that it makes sense. Any and all help would be greatly appreciated - thank you very much!
I am not really sure what you are trying to do, but the code below will assign the value of A(ii) to every element in the first column of B{ii}. I am just saying this based on your example, but your explanation is really unclear..
C = B;
for ii=1:numel(C)
C{ii}(:,1)=A(ii);
end
And you are getting an error on newArray(n) = [A(n),B{n}(2)]; because you are trying to assign a vector to a singleton dimension. Try a(1) = [1 2] and you will still get an error, and this is independent on what your cell dimensions are, etc.. a(1,:)=[1 2], however, might work if the 2nd dimension of a is 2.
Try:
newCellArray = cell(numel(B),2);
for n = 1:numel(B)
lenB = length(B{n}(2));
newA = repmat(A(n),lenB);
newArray = [newA,B{n}(2)];
newCellArray = [newCellArray; newArray];
end
I have a big cell array A=cell(a,b,c,d) and a row vector B with dimensions 1-by-b.
I want to build a loop in MATLAB that does the following:
for i=1:n
B = Calculate_row(input1,input2) %this is a function that creates my B row
A{a,:,c,i} = B(:)
end
anyway if I try to do A{a,:,c} = B(:) I receive the following error:
Expected one output from a curly brace or dot indexing expression, but there were b results.
And if I try to do A(a,:,c) = B(:) I receive the following error:
Conversion to cell from double is not possible.
Is there a way to do this? (I know a less elegant way that probably works would be to assign each value to the cell separately, but I would prefer not to do it).
One way to do this is to make B a cell array and then take advantage of comma-separated-lists:
B_cell = num2cell(B);
[A{a,:,c}] = B_cell{:} %// or [A{a,:,c,i}] = B_cell{:} if tim's comment is correct
Have a look at Loren Shure's article Deal or No Deal and also this answer for more.
The problem with your syntax, A{a,:,c} = B(:), is that the RHS (i.e. B(:)) is just one single matrix whereas the LHS is a comma-separated-list of b results. So you are basically requesting that 1 output be assigned to b variables and MATLAB doesn't like that, also hence the error message.
The problem with A(a,:,c) = B(:) is that indexing a cell array with () returns a cell array and you can't just assign a matrix (i.e. B(:)) to a cell array hence you second error.
In matlab I have the following 2 data structures b and c defined as follows.
b(1).b = struct('c',{'a', 'b', 'c'})
c(1).b = struct('c',{'b', 'a', 'c'})
Now I want to use ismember to find out if the elements of b(1).b.c are contained in c(1).b.c and if so, which indices of c(1).b.c each of the elements belong to.
For example: b(1).b(1).c = a, I want to backtrace this to structure c to find which index of structure c 'a' belongs to (it should return 2 since 'a' is the second element of structure c).
I have tried
[~, ind] = ismember({b(1).b.c},{c(1).b.c})
which has worked for me previously with a different data structure but I now receive the following error:
*Error using cell/ismember>cellismemberR2012a (line 192)
Input A of class cell and input B of class cell must be cell arrays of strings, unless one is a string.
Error in cell/ismember (line 56)
[varargout{1:max(1,nargout)}] = cellismemberR2012a(A,B);*
I am not sure why its not working. Does anybody know how to fix this?
Thanks.
Googling around did not show any possible solutions but there are couple options:
[~, ind] = ismember([{b(1).b.c}],[{c(1).b.c}])
and casting to cell array:
[~,idx]=ismember(struct2cell(b(1).b),struct2cell(c(1).b))
idx=reshape(idx,1,3);
For both output should be:
2 1 3
I found the following to work.
First assigning b(1).b.c to an array S and then comparing that with the data structure c using ismember.
S = [b(1).b.c]
S = S'
[~, ind] = ismember(S,{c(1).b.c})
I have found this to work.
Also,
[~, ind] = ismember([b(1).b.c}],[c(1).b.c])
does not give an error but all the values in ind are zero which is not true for the data.
Thanks all for your input!
I have a 10,000 x 65 Cell with 0's and 1's so for example if I type
C(1,1) I get [0] returned or likewise C(3,4) a [1] is returned
I need a way to turn every 0 into a blank cell and every 1 into a char t
I've tried the following with little success
[rows, cols] = size(M);
for i = 1:rows
for j = 1:cols
if strcmp(M(i,j), 1)
M(i,j) = 't';
end
end
end
It returns the same thing, I'm guessing its not recognising the 1's as strings. Any idea sort of simply doing the conversion straight in Excel.
thanks
You are not accessing the cell data-structure correctly.
First of all, if M really is a cell array, you will have to use M{i,j} to access the data.
What M(i,j)does is just create a sub-cell-array, which contains M{i,j} as entry.
Also strcmp isn't used correctly, if your cell array contains strings you should use strcmp(M{i,j}, '1').
If your cell array on the other hand contains integers, you would have to use: M{i,j}==1.
I have a cell array with strings and a numeric array in Matlab. I want every entry of the cell array to be concatenated with the number from the corresponding position of the array. I think that it can be easily solved using cellfun, but I failed to get it to work.
To clarify, here is an example:
c = {'121' '324' '456' '453' '321'};
array = 1:5
I would like to get:
c = {'1121' '2324' '3456' '4453' '5321'}
A special version of sprintf that outputs directly to a cell array is called sprintfc:
>> C = sprintfc('%d%d',[array(:) str2double(c(:))]).'
C =
'1121' '2324' '3456' '4453' '5321'
It is also a bit different in the way it handles array inputs, by preserving the shape.
You're correct, you can use cellfun – you just need to convert the array to a cell array as well using num2cell. Assuming array is a vector of integers:
c = {'121' '324' '456' '453' '321'};
array = 1:5;
c2 = cellfun(#(c,x)[int2str(x) c],c,num2cell(array),'UniformOutput',false)
which returns
c2 =
'1121' '2324' '3456' '4453' '5321'
In your case, you can also accomplish the same thing just using cell2mat and mat2cell:
c2 = mat2cell([int2str(array(:)) cell2mat(c.')],ones(1,length(array))).'
Another one-liner, this one without undocumented functions (with thanks to #chappjc for showing me the "format" input to num2str):
strcat(num2str(array(:),'%-d'),c(:)).'