I'm trying to plot something in a cell array, so I'm trying to convert one of the columns to a matrix.
I pulled out the column from the cell array and tried to do cell2mat on it to turn it into a matrix. However, cell2mat seems to just turn it into one long character array.
site(:,4)'; % Pull out column 4 from the cell array
cell2mat(ans); % Attempt to convert the cell into a matrix
The first part of the code gives me:
10.4 10.1 7.9 8.2
The second part of the code gives me:
10.410.17.98.2
How can I make the cell into a matrix that I can use to plot a graph?
It appears that your cell array contains strings, is that correct? In that case you don't use cell2mat, but str2double:
str2double(site(:,4).')
For example:
>> site = {'1', '2', '3', '4';
'1.1', '2.1', '3.1', '4.1'};
>> str2double(site(:,4).')
ans =
4.0000 4.1000
Assuming x is your abstract:
y = site(:,4)';
You can now make it a vector and plot it as such:
plot([y{:}])
Related
I transformed a structure S (with 13 fields and 96 rows, some fields are made of numbers, others of strings) into a cell array:
myCell= struct2cell(S);
So, I obtained a 3d cell array myCell 13x1x96, and I would like to transform it in a 2d cell array 96x13. Any suggestion is welcome!
A more general solution than what was suggested would employ the permute function:
B = permute(A,order) rearranges the dimensions of A so that they are in the order specified by the vector order.
...
permute and ipermute are a generalization of transpose (.') for multidimensional arrays.
In your case, running the command new_Cell = permute(myCell,[3,1,2]) would make the 13x1x96 96x13. As you can see, permute removes trailing singleton dimensions (resembling squeeze).
(Tested on MATLAB 2015b)
I have a 1x28 cell array called magV with each element containing a 246x247 matrix containing mostly NaNs.
I am trying to set up a for loop to go through each of these matrices and calculate a mean. The attempt so far:
mean_speeds = cell(1,28);
for x = 1 : 28
mean_speeds{x} = mean(magV{x});
end
This doesn't work; it just outputs another 1x28 cell array, with each element containing a 1x28 row of NaNs
What am I doing wrong?
Mean of anything containing NaN is a NaN. Remove . . .
mean(magV{x(~isnan(x))});
The mean function does not support NaN arguments. You can add a logic step to remove the invalid numbers then calculate the mean of the resulting array.
Or, you can use nanmean: see the nanmean Help Page
You can use cellfun to get rid of the loops.
If you want to ignore nan's
noNaN = cellfun(#(x) mean(x(~isnan(x))), magV, 'uni', 0);
If you want to treat them as zeros
zeroNaN = cellfun(#(x) sum(x(~isnan(x)))/numel(x), magV, 'uni', 0);
I have a 50x25 cell array in the variable raw_data. Each cell contains a 200x150 matrix. I have a few NaN values scattered between all those values and I want to set them to zeros to make sure they do not interfere at later stages.
I have tried the following:
raw_data(cellfun(#(x) any(isnan(x), raw_data, 'UniformOutput', false)) = 0
When running the script, I get "Function 'subindex' is not defined for values of class 'cell'". Can anyone help me, please?
Thanks in advance!
How about this:
cellfun(#(x) nansum(x,ndims(x)+1), raw_data, 'UniformOutput', false)
Note if you're certain you'll only have 2D matrices in raw_data you can replace the ndims(x)+1 with 3.
The idea is to use nansum to sum along the 3rd dimension as this will preserve the shape of the first 2 dimensions and luckily nansum seems to convert NaN to 0 when all the elements being summed are NaN
I have a 4554 x 1 vector of type double, called company_info,ind_vec. I also have a another 25 x 1 vector which is of type cell array called groups.industy_labels.
groups.industy_labels contains a list of codes which are numeric.
company_info,ind_vec contains the same numeric codes.
I was planning on doing the below, where I use ismember to return the indices for each numeric code in groups.industy_labels and then do a sum on another vector which is realted to company_info,ind_vec, i.e. another 4554 x 1 vector.
[~, index_sub] = ismember(company_info.ind_vec, groups.industy_labels);
groups.industy_exps(:, 1) = accumarray(index_sub, pwgt , [], #sum, 0);
However Matlab is telling me that ismember only takes cell arrays of string. Is there another way of doing this?
Actually the error message is a bit deceptive as you can use ismember for numeric values:
x=[1 3]
y=[1 2]
ismember(x,y) %This will work
You can also use it for cell arrays, but only for strings:
x=[{'a'},{'c'}]
y=[{'a'},{'b'}]
ismember(x,y) %This will work
x=[{1},{3}]
y=[{1},{2}]
ismember(x,y) %This will fail
So in your case, you would want to use it on 2 numeric vectors, rather than 1 numeric vector and 1 cell array:
x=[1,2] %Numeric vector
y=[{1},{2}] %Cell array
y_numeric = [y{:}] %Made into a numeric vector
ismember(x,y_numeric) %This will work
Note that this assumes that each entry in the cell array only contains a number.
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.