I have a 73 x 1 cell, each of those cells contains a 16 x 1 cell and each of those cells is an image. Is there an easy way I can convert this into one big array of cells containing only the images? Many thanks.
If C is your cell, use B = [C{:}] to create a 16×73 cell B with every column one of your original 16×1 cell elements. This works, because C{:} accesses every element in cell C and the brackets ([ ]) group all these elements into one array again. This is possible, because every element in C is of the same type and size.
Use B = B(:) to get a 1168×1 cell (73*16=1168), if you want. Either way, B{n} accesses the n-th image.
Related
I have a cell array called d_O3 of size 3x15.
Here's what the first row looks like:
10x29 cell
31x29 cell
40x29 cell
...
I want to turn column one inside each cell into a column array and vertically concatenate them all together.
Here's what I've done so far:
years = 1999:2013; % The 15 columns
m = 1; % Row 1. I'd like to be able to run a loop for the other rows to do the same thing, but I haven't figured out how.
for y = 1:numel(years)
data{y,1} = d_O3{m.y}(:,1);
end
This creates a 15x1 cell that looks like this inside:
31x1 cell
40x1 cell
42x1 cell
...
Inside each cell (i.e. inside 31x1), there's a column array of strings. But I want it to all concatenate together so it looks like:
06-029-0001-88101
06-073-0010-88101
...
In other words, I want to make vertically concatenate all the cells above.
I can do it by doing the following:
vertcat(data{1,1},data{2,1},...)
But that would mean typing out data{i,1} 15 times. What's an easier way?
vertcat(data{1:15,1})
or
vertcat(data{:,1})
It creates a comma separated list which is passed to vertcat.
I have P being a 17x1 cell, each cell representing a subject. Each of the 17 cells within P is a 3x1 cell, representing contrast images for each subject. So basically there are 17 subjects, 3 contrast images per subject. So to index the 3rd contrast images of the 14th subject, I would do P{14,1}{3,1}. However, I would like to turn P into 3*17 x 1 cell (or 51x1 cell) instead. This means there are no nested cells within each cell in P. So P would be something like this:
Subject1/contrast1.img
Subject1/contrast2.img
Subject1/contrast3.img
Subject2/contrast1.img
Subject2/contrast2.img
Subject3/contrast3.img
...
Subject17/contrast3.img
Could anyone tell me how this may be accomplished?
How about a oneliner?
a = {{1;2};{3;4};{5;6};{7;8}}
b = vertcat(a{:})
It is a basic problem but I am not so much experienced in Matlab(Guide).
What I have now is a cell array called Z with 21x2 elements: 21 rows 2 columns.
What I would like to do is to get only the first column (to show only 21x1).
Then, in this column there is a list of names. Inside the 21 rows of this cell there are repeated names. I would like to run through each row of this 21x1 column, detect which are repeated. The repeated ones should be printed in the uitable in a white colour.
Any ideas?
I believe this should deal with the core of your question:
A={'abc' 6;'de' 7;'abc' 8};
[C, ia] = unique(A(:,1));
idx = setdiff(1:size(A,1),ia);
A(idx,1)
This code will list all duplicates.
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}
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.