MATLAB Populate matrix with elements from a cell array - matlab

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.

Related

Trouble with outputting a mean for each element in a cell array (MATLAB)

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);

Replace NaN with zeros in cell array in MATLAB

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

How to compare two cell elements in matlab?

I am using two cells for storing the targeted and the expected value of a neural network process in matlab. I have used two 1*1 cell array for storing the values respectively. And here is my code.
cinfo=cell(1,2)
cinfo(1,1)=iter(1,10)%value is retrieved from a dataset iter
cinfo(1,2)=iter(1,11)
amp1=cinfo(1,1);
amp2=cinfo(1,2);
if amp1 == amp2
message=sprintf('NOT DETECTED BY THE DISEASE');
uiwait(msgbox(message));
But when i run the above code, the get the following error :
??? Undefined function or method 'eq' for input arguments of type 'cell'.
Error in ==> comparison at line 38
if amp1 == amp2
How to solve this problem?
The problem is how you indexed things. A 1x1 cell array does not make a lot of sense, instead get the actual element in the single cell, by indexing with curly brackets:
amp1=cinfo{1,1}; # get the actual element from the cell array, and not just a
amp2=cinfo{1,2}; # 1x1 cell array by indexing with {}
if (amp1 == amp2)
## etc...
However, note that if amp1 and amp2 are not scalars the above will act weird. Instead, do
if (all (amp1 == amp2))
## etc...
Use isequal. That will work even if the cell's contents have different sizes.
Example:
cinfo=cell(1,2);
cinfo(1,1) = {1:10}; %// store vector of 10 numbers in cell 1
cinfo(1,2) = {1:20}; %// store vector of 20 numbers in cell 2
amp1 = cinfo(1,1); %// single cell containing a length-10 numeric vector
amp2 = cinfo(1,2); %// single cell containing a length-20 numeric vector
if isequal(amp1,amp2)
%// ...
In this example, which parallels your code, amp1 and amp2 are cell arrays consisting of a single cell which contains a numeric vector. Another possibility is to directly store each cell's contents into amp1, amp2, and then compare them:
amp1 = cinfo{1,1}; %// length-10 numeric vector
amp2 = cinfo{1,2}; %// length-20 numeric vector
if isequal(amp1,amp2)
%// ...
Note that even in this case, the comparisons amp1==amp1 or all(amp1==amp2) would give an error, because the vectors have different sizes.

MATLAB cell2mat concatenating numbers together

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{:}])

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.