I'm trying to append [1 2.2] to {'foo' 'ba'} and get:
'foo' 'ba'
1 2.200000
I'm nearly there:
>> A = {'foo' 'ba'}
A =
1×2 cell array
'foo' 'ba'
>> b = [1 2.2]
b =
1.000000000000000 2.200000000000000
>> [A;b]
Error using vertcat
Dimensions of matrices being concatenated are
not consistent.
>> [A;num2cell(b)]
ans =
2×2 cell array
'foo' 'ba'
[ 1] [2.200000000000000]
How to get rid of the []?
[A;num2cell(b)] is the correct implementation.
You can try doing
[A(1);b(1)]
ans =
'foo'
[ 1]
Notice the vector b element 1 represented as [ 1] which are equalivalent.
A = {'foo' 'ba'} ;
b = {'1' '2.2'} ;
[A ;b]
or
vertcat(A,b)
Related
I'd like to insert a column of character to a matrix in MATLAB.
For example, we want to reach from first matrix to the second matrix:
first_matrix = [2 3; 4 5; 1 7]
second_matrix = [c 2 3; c 4 5; c 1 7]
In fact the reason is that, I have a output.txt file from a software. In that file, I should select a matrix in it, and change matrix column order. After doing this, i.e. reach to first_matrix, the output in form of second_matrix should be used in another software. So, finally I should save it in a text file format for second software.
You cannot do with this numeric arrays. The possible ways to do this are:
Using a categorical array i.e.
>> second_matrix = [num2cell(repmat('c',3,1)) categorical(first_matrix)]
ans =
3×3 categorical array
c 2 3
c 4 5
c 1 7
Using a character array i.e.
>> second_matrix = [repmat('c ',3,1) num2str(first_matrix)]
second_matrix =
3×7 char array
'c 2 3'
'c 4 5'
'c 1 7'
Using a string array (requires ≥ R2016b) i.e.
>> second_matrix = [repmat("c",3,1) first_matrix] %in ≥ R2017a
% second_matrix = [repmat(string('c'),3,1) first_matrix] %in ≥ R2016b
second_matrix =
3×3 string array
"c" "2" "3"
"c" "4" "5"
"c" "1" "7"
Using a cell array i.e.
>> second_matrix = [num2cell(repmat('c',3,1)) num2cell(first_matrix)]]
second_matrix =
3×3 cell array
{'c'} {[2]} {[3]}
{'c'} {[4]} {[5]}
{'c'} {[1]} {[7]}
Using a symbolic array (requires Symbolic Math Toolbox) i.e.
>> second_matrix = [repmat(sym('c'),3,1) first_matrix]
second_matrix =
[ c, 2, 3]
[ c, 4, 5]
[ c, 1, 7]
Consider the script
syms a b;
f = a.*b;
a = [1 2];
b = [0 2];
subs(f)
This yields a vector as output ([0, 4]), but the intended output is a scalar (4), given the element-wise multiplication that should be performed.
How can I properly utilize vectors when substituting symbolic functions?
I believe you're making two mistakes. The first is that you're overwriting your symbolic variables with double arrays, so you're not actually calling subs for a symbolic object:
>> syms a;
>> class(a)
ans =
sym
>> a = [1 2];
>> class(a)
ans =
double
The second is that preventing this will still give a wrong answer:
>> syms a b;
>> f = a.*b
f =
a*b
>> subs(f,{a,b},{[1, 2], [0,2]})
ans =
0 4
That's because, as you see in the printed version of f, the symbolic engine treats a and b as scalars.
So to do what you probably want to do you need to define your syms to be 2-element arrays:
> a = sym('a',[1 2])
a =
[ a1, a2]
>> b = sym('b',[1 2])
b =
[ b1, b2]
>> f = a.*b
f =
[ a1*b1, a2*b2]
>> subs(f,[a,b],[[1,2],[0,2]])
ans =
0 4
But anyway, as you can see, the result is still an array since .* (or symbolic *) is elementwise multiplication. In order to get a scalar product, use sym/dot:
>> dot(a,b)
ans =
b1*conj(a1) + b2*conj(a2)
>> subs(dot(a,b),[a,b],[[1,2],[0,2]])
ans =
4
In mat-lab I have Binary 10x10 matrix
I want to convert this into a 10x10 decimal matrix. How I can do that ?
Edit: r is 10x10 binary matrix.
According to your snapshot, we can infer that you have a double type matrix contains only "1" and "0" as value.
Therefore, I reproduce and solve your case like this:
>> r = [11111010, 11111111; 1010101, 101]
result = cellfun(#(x) bin2dec(num2str(x)), num2cell(r))
r =
11111010 11111111
1010101 101
result =
250 255
85 5
Or, you don't have to use cellfun(), you can just use arrayfun() without converting array/matrix to cell array, like this:
>> r = [11111010, 11111111; 1010101, 101]
result = arrayfun(#(x) bin2dec(num2str(x)), r)
Or, if we assumed you have binary type (logical) cell array, this could be a solution:
r = {[1 1 1 1 1 0 1 0], [1 1 1 1 1 1 1 1]; [1 0 1 0 1 0 1], [1 0 1]}; %// Double type binary valued cell array
r = cellfun(#(x) logical(x), r, 'UniformOutput', false); %// You asserted binary type cell array is given
result = cell2mat(cellfun(#(x)( sum(pow2(length(x)-1:-1:0) .* x) ), r, 'UniformOutput', false));
Used built-in functions: cellfun(), bin2dec(), num2str(), num2cell(), arrayfun() and cell2mat().
However I try to test if x is [] I fail, seems that it should be trivial, but can't figure out how to do it.
if I run x = rmi('get',subsystemPath);
ans = []
I've tried
x == []
x
isempty(fieldnames(x))
isEmpty(x)
but nothing works
function requirements = GetRequirementsFromSubsystem(subsystemPath)
x = rmi('get',subsystemPath);
if(isempty(fieldnames(x))) %%%%%%%%%%%%%%%%<------
requirements = 0;
else
requirements = {x.description}; % Fails if do this without a check
end
end
Any ideas?
x is a struct, right? In that case, according to this posting on the MATLAB newsgroup, there are two kinds of emptiness for structs:
S = struct() => no fields
isempty(S) is FALSE, because S is a [1 x 1] struct without fields
S = struct('Field1', {}) => fields, but no data
isempty(S) is TRUE, because S is a [0 x 0] struct with fields
For me, isempty(fieldnames(S)) works only for the first case in Octave, at least.
If x on the other hand, is an array, not a struct, then isempty(x) should work.
>> S = struct()
S =
scalar structure containing the fields:
>> isempty(S)
ans = 0
>> isempty(fieldnames(S))
ans = 1
>> S = struct('Field1',{})
S =
0x0 struct array containing the fields:
Field1
>> isempty(S)
ans = 1
>> isempty(fieldnames(S))
ans = 0
>> x = []
x = [](0x0)
>> isempty(x)
ans = 1
I'm trying to append an element at the end of a 2D cell array row. My code is:
b = cell(5, 0)
b(1) = {b(1, :), 2} % Trying to append at the end of the first row
This gives me the error: error: A(I) = X: X must have the same size as I
I've also tried various other forms, such as:
b = cell(5, 0)
b(1, end+1) = 2 % Ok, inserts 2 at [1,1]
b(2, end+1) = 3 % No, inserts 3 at [2,2] instead of [2, 1]
It seems that you are confused with cell array indexing.
If you want to append elements at the end of a row in a matrix (in your case, a cell array), you must still make sure that all rows are of the same size after the assignment, otherwise you'll trigger an error about mismatching dimensions.
Instead of b(1) = {b(1, :), 2}, the following should work:
b(1, end + 1) = 2
Alternatively, if you want to append an entire column array of cells to b, use horizontal concatenation, for example:
b = [b, {2; 3; 4; 5; 6}];
This should append a single cell at the end of each row of b.
The reason the element gets inserted at [2, 2] and not [1, 1] is that by the time you try to insert the second element, the value denoted by end has increased from 0 to 1.
The following should do what you need:
>> b = cell(5, 0)
b =
Empty cell array: 5-by-0
>> b(1,1) = {2}
b =
[2]
[]
[]
[]
[]
>> b(2,1) = {3}
b =
[2]
[3]
[]
[]
[]
>>