Concatenate symbol with cell array values in Matlab - matlab

I have a cell of double {52x1}, I would like to concatenate to each element the symbol ±.
The problem I am having is that sprintf doesn't support the Matlab code \pm for calling the symbol.
Any help is welcome!

\pm is a TeX/LaTeX command, which gives ± only if the interpreter used by Matlab understands LaTex. This occurs for example in axis labels when the TickLabelInterpreter property is set to 'tex'.
In sprintf you can directly use the ± symbol (code point 177). For example,
x = num2cell(rand(5,1)); % cell array of numbers
sprintf('±%f\n', [x{:}])
or
sprintf([177 '%f\n'], [x{:}])
gives
ans =
±0.126987
±0.913376
±0.632359
±0.097540
±0.278498
Note that I had to transform the cell array of numbers to a numeric vector in order to pass it to sprintf. Consider defining the data directly as a numeric vector to avoid that step.
If you want a cell array of strings as result:
cellfun(#(t) sprintf([177 '%f\n'], t), x, 'UniformOutput', false)

Related

How to split cell array values into two columns in MATLAB?

I have data in a cell array as shown in the variable viewer here:
{[2.13949546690144;56.9515770543056],
[1.98550875192835;50.4110852121618],
...}
I want to split it into two columns with two decimal-point numbers as:
2.13 56.95
1.98 50.41
by removing opening and closing braces and semicolons such as [;]
(to do as like "Text to columns" in Excel).
If your N-element cell array C has 2-by-1 numeric data in each cell, you can easily convert that into an N-by-2 numeric matrix M like so (using the round function to round each element to 2 significant digits):
M = round([C{:}].', 2);
The syntax C{:} creates a comma-separated list of the contents of C, equivalent to C{1}, C{2}, ... C{N}. These are all horizontally concatenated using [ ... ], then the result is transposed using .'.
% let's build a matching example...
c = cell(2,1);
c{1} = [2.13949546690144; 56.9515770543056];
c{2} = [1.98550875192835; 50.4110852121618];
% convert your cell array to a double array...
m = cell2mat(c);
% take the odd rows and place them to the left
% take the even rows and place them to the right
m = [m(1:2:end,:) m(2:2:end,:)];
% round the whole matrix to two decimal digits
m = round(m,2);
Depending on your environment settings, you may still see a lot of trailing zeros after the first two decimal digits... but don't worry, everything is ok (on the precision point of view). If you want to display only the "real" digits of your numbers, use this command:
format short g;
you should use cell2mat
A={2.14,1.99;56.95,50.41};
B=cell2mat(A);
As for the rounding, you can do:
B=round(100*B)/100;

Avoid looping in matlab when creating cells containing cell arrays

I'm trying to create a map that has two-element cell arrays as values. Map expects that keys and values have the same number of elements. This code packs those cell arrays into cells in a loop, but I'm suspecting that it can be simplified somehow. Example code:
cells1={'foo1';'foo2';'foo3'};
cells2={'bar1';'bar2';'bar3'};
cells3={'baz1';'baz2';'baz3'};
values=cell(size(cells1));
for ii=1:size(cells1,1)
values{ii}={{cells2{ii},cells3{ii}}};
end
keys=cells1;
containers.Map(keys,values);
you can use vector concatenation and num2cell with 2nd dimension argument (twice if you want to obtain identical result):
% your code
cells1={'foo1';'foo2';'foo3'};
cells2={'bar1';'bar2';'bar3'};
cells3={'baz1';'baz2';'baz3'};
values=cell(size(cells1));
for ii=1:size(cells1,1)
values{ii}={{cells2{ii},cells3{ii}}};
end
% simplified
c = num2cell(num2cell([cells2,cells3],2),2);
% you can also do c = num2cell([cells2,cells3],2); which isn't identical but may be suficcient
isequal(c,values) % yes

matlab: Adding square brackets to all individual numerical values within cell arrays

I wish to add a cell array (Ma) into another cell array. However both needs to be of the same format (individual cells in square brackets).
For instance I have an array..
Ma{1,:}.'
ans =
Columns 1 through 8
'83.6' '85.2' '91' '87.9' '91.8' '86.3' '90.6' '90.2'
How do i add square brackets to all the numerical values of very individual cells?
Below is what i wish to obtain, it is also a 1x8 cell.
ans =[83.6] [85.2] [91] [87.9] [91.8] [86.3] [90.6] [90.2]
Your cell values are strings (you can tell by the quote marks ' surrounding the values). You wish to convert them to numerical values ("add square brrckets around them" as you put it).
To convert string to double you can use str2double command:
M = str2double( M{1,:} );
You don't need to add the square brackets yourself. This just means that it is a numerical value in a cell.
In order to achieve this you should do the following, using both the num2cell as str2double functions:
newM = num2cell(str2double(Ma{1,:}))

using ismember for numeric values

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.

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.