How to add element to cell array including case it is empty? - matlab

Is it possible to add element to cell array including case when it is currently empty and or not exist?
All of these methods work
>> C={12}
C =
[12]
>> C=[C;13]
C =
[12]
[13]
>> C{end+1}=14
C =
[12]
[13]
[14]
But all of them require C exists.
Neither work if C does not exist. Is it possible to do so?

Related

How to overload convert

I have two types A and B. I have written B(a::A) and convert(::Type{B}, a), but this does not immediately make Array{B}(as::Array{A}) work. Must I write this method as well? According to
the documentation, I would expect Julia would handle the rest for me.
struct A end
struct B end
B(a::A) = B();
convert(::Type{B}, a) = B(a);
# These work
B(A());
convert(B, A());
# This doesn't
Array{B}([A()]);
This is the error
ERROR: LoadError: MethodError: Cannot `convert` an object of type A to an object of type B
Closest candidates are:
convert(::Type{T}, !Matched::T) where T at essentials.jl:154
B(::A) at /home/mvarble/test.jl:3
Stacktrace:
[1] setindex!(::Array{B,1}, ::A, ::Int64) at ./array.jl:767
[2] copyto! at ./abstractarray.jl:753 [inlined]
[3] copyto! at ./abstractarray.jl:745 [inlined]
[4] Type at ./array.jl:482 [inlined]
[5] Array{B,N} where N(::Array{A,1}) at ./boot.jl:427
[6] top-level scope at none:0
[7] include at ./boot.jl:326 [inlined]
[8] include_relative(::Module, ::String) at ./loading.jl:1038
[9] include(::Module, ::String) at ./sysimg.jl:29
[10] exec_options(::Base.JLOptions) at ./client.jl:267
[11] _start() at ./client.jl:436
You have to add a method to convert from Base, and not define a new convert function in the current module. Therefore you should write:
Base.convert(::Type{B}, a) = B(a)
or
import Base: convert
before defining convert as you did in your code.

getting list of values with list of keys for dictionary in Matlab

Suppose I use containers map to create a dictionary in MATLAB which has the following map:
1-A;
2-B;
3-C;
Denote the dictionary as D.
Now I have an input list [2,1,3], and what I am expecting is [B,A,C]. The problem is, I can't just use [2,1,3] as the input list for D, but only input 2,1 and 3 one by one for D and get B, A, C each time.
This can get the job done but as you can see, it's a bit less efficient.
So my question is: is there anything else I can do to let the dictionary return the whole list at the same time?
As far as I can find there is no one-step solution like python's dict.items. You can, however, get in a few lines. mydict.keys() gives you the keys of the dict as a cell array, and mydict.values() gives you the values as a cell array, so you can (in theory) combine those:
>> mykeys = mydict.keys();
>> myvals = mydict.values();
>> mypairs = [mykeys',myvals']
mypairs =
3×2 cell array
'A' [1]
'B' [2]
'C' [3]
However, in principle maps are unordered, and I can't find anything in the MATLAB documentation that says that the order returns by keys and the order returned by values is necessarily consistent (unlike Python). So if you want to be extra safe, you can call values with a cell array of the keys you want, which in this case would be all the keys:
>> mykeys = mydict.keys();
>> myvals = mydict.values(mykeys);
>> mypairs = [mykeys',myvals']
mypairs =
3×2 cell array
'A' [1]
'B' [2]
'C' [3]

Add a new element to the end of an existing cell array

As the title already mentions, how is it possible to add a new cell array 1x1 at the end of an existing cell array, let's call him Q, which is a cell array 1x3256?
If you mean adding a single cell to the end (i.e. so your 1-by-3256 cell array becomes a 1-by-3257 cell array) then:
Q{end+1} = []
and you can replace [] with your value directly
Alternatively:
Q(end+1) = {[]}
Adding to Dan's answer, in case you have a cell that is not a single dimension cell, you might want to add a full row, for example. In that case, access the cell as an array using ().
>> c = { 1, 'a'; 2, 'b'}
c =
[1] 'a'
[2] 'b'
>> c(end+1,:) = {3,'c'}
c =
[1] 'a'
[2] 'b'
[3] 'c'

How to add rows in between cell array in Matlab?

I have two cell arrays.
A(290*6) and B(300*6);
First column in two arrays are identical. I compared first column of two cell arrays using 'ismember'. I want to do that ; where cell elements are missing in cell array(A), I have to add a row where element is missing. Is it possible in Matlab?
It's not easy to insert rows into an existing matrix or cell array; it's easier to construct a new one and fill it appropriately.
Find the locations of the contents of the first column of A in the cell array B:
[aa,bb] = ismember([A{:,1}],[B{:,1}]);
Create a new empty cell array:
C = cell(length(B),size(A,2))
Fill it:
C(:,1)=B(:,1)
C(bb,2:end) = A(aa,2:end);
For example, given this A ("3" row missing)
[1] [3]
[2] [5]
[4] [3]
And this B:
[1]
[2]
[3]
[4]
This returns:
[1] [3]
[2] [5]
[3] []
[4] [3]
To fill the empty spaces with the previous row (this will only work if the empty rows are non-consecutive and the first row of C is non-empty):
n = setdiff(1:length(C),bb)
C(n,2:end) = C(n-1,2:end);
I think you can directly use the second output of setdiff
[d,i] = setdiff(B(:,1),A(:,1))
i will tell you where the rows in A are that are missing.

How to filter logical column?

I need to get only false value of a logical column, I' ve column data that return this:
K>> data(:,4)
ans =
[1]
[1]
[0]
[0]
[0]
[0]
[0]
I tried this but don't work
data= data(strcmp(data(:,4), {false}), :);
Output:
data =
Empty cell array: 0-by-4
From your data, it appears you want this:
~([data{:,4}])
or maybe this
find(~([data{:,4}]))