create a paired value cell array from existing vectors - matlab

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}

Related

How to sum up values in corresponding indexes of cells?

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.

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.

subindex into a cell array of strings

I have a 6 x 3 cell (called strat) where the first two columns contain text, the last column has either 1 or 2.
I want to take a subset of this cell array. Basically select only the rows where the last column has a 1 in it.
I tried the following,
ff = strat(strat(:, 3), 1:2) == 1;
The error message is,
Function 'subsindex' is not defined for values of class 'cell'.
How can I index into a cell array?
Cell arrays are accessed through braces {} instead of parentheses (). Then, as a 2nd subtlety, when pulling values out of a cell arrays, you need to gather them...for numerics you gather them into regular arrays using [] and for strings you gather them into a new cell array using {}. Confusing, eh?
ff = { strat{ [strat{:,3}]==1 , 1:2 } };
Gathering into cell arrays this way can often give the wrong shape when you're done. So, you might try something like this
ind = find([strat{:,3}]==1); %find the relevant indices
ff = {{strat{ind,1}; strat{ind,2}}'; %this will probably give you the right shape

Store a particular set points in a new cell array

I had asked a similar question few weeks back but I think that question was not framed properly so please excuse me for asking the same question again
I have a column vector with values
32.5
25.8
25.91
25.92
16.52
16.7
Now I want to create a cell array such that my first cell contains the first value, my second cell array contains value from 25.8 to 25.92 and finally my third cell array contains the values 16.52 and 16.7 .
How can I solve this problem.
Since you didn't explain why you want the vector split up, I assume the specified division is arbitrary. The following should do what you want, assuming v is the column vector:
c = {v(1) v(2:4) v(5:6)};
Basically you are constructing three new vectors out of pieces of the original, and putting them together into a cell array.
This is a way with for loop.
A = [...
32.5
25.8
25.91
25.92
16.52
16.7];
[U,~,ic] = unique(floor(A));
B = cell(length(U),1); % Result
for k = 1:length(A)
B{ic(k)} = [B{ic(k)} A(k)];
end
Thank to unique command the result is even sorted.

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.