Using cell arrays and getting too many input arguments error - arrays

I'm a beginner in MATLAB. I have my function testnetwork:
function result = TestNetwork(network, input)
result = input;
b= [-1 -1 -1 -1 ];
% Iterate over all the couches
for i=1:length(network.couches)
result = network.activation(matrix_multiplication_same_dimension(network.couches{i} , vertcat ( result , b)));
end
end
and this is my main script:
% initialis a cell of zeros for example output = zeros_quat(zeros(1, 2)) is %equal to [0 0 0 0] [0 0 0 0]
output = zeros_quat(zeros(10, size(testset,2)));
%
for i = 1:size(testset, 2)
%testset is a cell of arrays size (81 * 180)
output {:,i} = TestNetwork(network, testset{:,i});
end
end
I get the error too many input arguments. I don't know what the problem is.

This line is the problem:
output {:,i} = TestNetwork(network, testset{:,i});
When you de-reference the cell array testset using curly braces {} with multiple entries, it takes the individual cells of the cell array and returns all of them as separate arguments:
a = { [ 1 2], [3 4] };
a{1}
ans =
1 2
a{1,:}
ans =
1 2
ans =
3 4
Note the two instances of ans in the second evaluation. I suspect that what you really want is a reference to a single cell on both sides of the equation:
output{i} = TestNetwork(network, testset{i});

Related

Syntax understanding in task with matlab

can comeone help to understand in MAtlab this:
k=2
n = (0:-1:-4)+k
the result; 2 1 0 -1 -2
how it works?
You are dealing with a colon operator and a vectorized sum at the same time. Let's split the problem into smaller, stand-alone problems:
In Matlab, if you add or subtract between a scalar value to a matrix, the arithmetic operation is performed on all the elements of the matrix, in a vectorized way. Example:
A = [1 2; 3 4]; % 2-by-2 matrix
S1 = A + 2 % output: S1 = [3 4; 5 6]
B = [1 2 3 4] % 1-by-5 matrix, also called column vector
S2 = B - 5 % output: S2 = [3 4 5 6]
The column operator in Matlab can be used in many situation: indexing, for iterations and vector creation. In your case, its purpose is the third one and it's syntax is START(:STEP):END. The default STEP, if not specified, is 1. The START and END parameters are never exceeded. Example:
A = 1:5 % output: A = [1 2 3 4 5]
B = -2.5:2.5:6 % output: B = [-2.5 0 2.5 5]
C = 1:-1:-5 % output: C = [1 0 -1 -2 -3 -4 -5]
D = -4:-2:0 % output: D = []
In all the programming languages, an operator precedence criterion is defined so that a one-liner calculation that uses multiple operators is atomized into smaller calculations that respect the given priority, unless parentheses are used to redefine the default criterion... just like in common maths. Example
A = 2 * 5 + 3 % output: A = 13
B = 2 * (5 + 3) % output: B = 16
Let's put all this together to provide you an explaination:
n = (0:-1:-4) + k
% vector creation has parentheses, so it's executed first
% then, the addition is executed on the result of the first operation
Let's subdivide the calculation into intermediate steps:
n_1 = 0:-1:-4 % output: n_1 = [0 -1 -2 -3 -4]
n_2 = n_1 + k % output: n_2 = [2 1 0 -1 -2]
n = n_2
Want to see what happens without parentheses?
n = 0:-1:-4+k % output: n = [0 -1 -2]
Why? Because the addition has priority over the colon operator. It's like writing n = 0:-1:(-4+k) and adding k to the END parameter of the colon operator. Let's subdivide the calculation into intermediate steps:
n_1 = -4 + k % output: n_1 = -2
n_2 = 0:-1:n_1 % output: n_2 = [0 -1 -2]
n = n_2
Basic Matlab syntax, you're dealing with range operators. There are two patterns:
[start]:[end]
and
[start]:[step]:[end]
Patterns like this result in arrays / vectors / "1D matrices".
In your example, you will get a vector first, stepping through the numbers 0 to -4 (step == -1). Then, you are adding k == 2 to all numbers in this vector.
octave:1> k = 2
k = 2
octave:2> n = (0:-1:-4)+k
n =
2 1 0 -1 -2
octave:3> 0:-1:-4
ans =
0 -1 -2 -3 -4
The parenthesizes expression determines an array. The the first number there is the first element, the second is the step and the last one is the last element. So the parenthesizes returns 0 -1 -2 -3 -4. Next we add k=2 to each element that results in 2 1 0 -1 -2

Convert vector of numbers to string without removing the zeros at start and end

I have a vector of integers and I want to convert it into one number.For example:
h = [1 2 4 3]; % after conversion res= 1243
k = [ 0 0 0 0]; % after conversion res=0000
l = [0 0 2 1]; % after conversion res=0021
I tried to use this method
b = [1 2 4 3];
res= str2num(strrep(num2str(b'), ' ', ''))
This method works fine when there is no zeros at start or end of the vector. But when there is zeros at end or start, str2num is removing them. For example, for vector k, res=0. For vector l, res=21.
How can I keep the zeros at the start and end?
At least for Octave, you can use a vectorized sprintf to print each array element into a string.
>> a = sprintf('%d', [0 0 1 2 3 4 0 0])
a = 00123400
very likely the same code would work too for MATLAB. Unfortunately I can't test it right now.
The easiest certainly is to convert your numbers directly to characters by offsetting them according to the ASCII-character table with 48:
char(h + 48)
char(k + 48)
char(l + 48)
If your input is a matrix (what I'd recommend) you additionally need to cellstr to convert the character matrix to a cell array of strings:
h(1,:) = [1 2 4 3];
h(2,:) = [0 0 0 0];
h(3,:) = [0 0 2 1];
out = cellstr( char(h + 48) )
You can make use of num2str() and regular expressions, regexprep(), to achieve the result.
s1 = num2str(h)
s2 = num2str(k)
s3 = num2str(l)
The array is directly converted into a string and results in the following output.
s1 = 1 2 4 3
s2 = 0 0 0 0
s3 = 0 0 2 1
Now, we can use regular expressions to find white spaces and replace it with ''
s1 = regexprep(s1,'\s+', '')
s2 = regexprep(s2,'\s+', '')
s3 = regexprep(s3,'\s+', '')
This produces the following result.
s1 = 1243
s2 = 0000
s3 = 0021
In 16b you can use the new string class. The string constructor will convert the numeric array to a string array of the same dimensions. Join will concatenate the elements together.
>> b = [1 2 4 3];
>> string(b).join('')
ans =
string
"1243"
h = [1 2 4 3];
k = [0 0 0 0];
l = [0 0 2 1];
strrep(num2str(h), ' ', ''); % this will return '1234'
strrep(num2str(k), ' ', ''); % this will return '0000'
strrep(num2str(l), ' ', ''); % this will return '0021'

MATLAB - Avoid repeated values in a vector inside cell arrays and take next

This is the problem:
I have a cell array on the form indx{ii} where each ii is an array of size 1xNii (this means the arrays have different size).
And another cell array on the form indy{jj} where each jj is an array of the same size as ii.
The question is that I would like to create a function evaluates the values in the arrays of indx{:} and take the first one that is not repeated, and if is a repeated value then take the next.
I will try to explain with an example. Suppose we have indx and indy that are the cell arrays:
indx{1} = [1 3 2 7];
indx{2} = [3 8 5];
indx{3} = [3 6 2 9];
indx{4} = [1 3 4];
indx{5} = [3 1 4];
indy{1} = [0.12 0.21 0.31 0.44];
indy{2} = [0.22 0.34 0.54];
indy{3} = [0.13 0.23 0.36 0.41];
indy{4} = [0.12 0.16 0.22];
indy{5} = [0.14 0.19 0.26];
What I want the code to do is take the first value and is not repeated in indx and the equivalent in indy. So the answer for the example should be:
ans=
indx{1} = 1;
indx{2} = 3;
indx{3} = 6;
indx{4} = 4;
indx{5} = [];
indy{1} = 0.12;
indy{2} = 0.22;
indy{3} = 0.23;
indy{4} = 0.22;
indy{5} = [];
In ans, for indx{1} the code takes 1 because is the first and it's not repeated and takes the equivalent value in indy. Then for indx{2} it takes 3 because is the first value and is not repeated as first value in any array before. But for ind{3} it takes 6, because the first value that is 3 is repeated, and takes the equivalent value to 6 in indy which is 0.23. For ind{4} the first and second value they are already repeated as first values so the code takes 4 and its equivalent in indy. And last, for indx{5} since all values are already repeated the code should take no value.
indx{1} = [1 3 2 7];
indx{2} = [3 8 5];
indx{3} = [3 6 2 9];
indx{4} = [1 3 4];
indx{5} = [3 1 4];
indy{1} = [0.12 0.21 0.31 0.44];
indy{2} = [0.22 0.34 0.54];
indy{3} = [0.13 0.23 0.36 0.41];
indy{4} = [0.12 0.16 0.22];
indy{5} = [0.14 0.19 0.26];
indx2 = NaN(numel(indx),1);
indx2(1) = indx{1}(1);
indy2 = NaN(numel(indy),1);
indy2(1) = indy{1}(1);
for ii = 2:numel(indx)
tmp1 = indx{ii}'; % get the original as array
tmp2 = indy{ii}';
if numel(tmp1)>numel(indx2)
tmp3 = [indx2;NaN(numel(tmp1)-numel(indx2),1)];
tmp4 = [indx2;NaN(numel(tmp1)-numel(indx2),1)];
else
tmp1 = [tmp1;NaN(numel(indx2)-numel(tmp1),1)];
tmp2 = [tmp2;NaN(numel(indx2)-numel(tmp2),1)];
tmp3 = indx2;
tmp4 = indy2;
end
tmp5 = ~ismember(tmp1,tmp3); % find first non equal one
tmp6 = find(tmp5,1,'first');
indx2(ii) = tmp1(tmp6); % save values
indy2(ii) = tmp2(tmp6);
end
N = numel(indx2);
indx2 = mat2cell(indx2, repmat(1,N,1));
N = numel(indy2);
indy2 = mat2cell(indy2, repmat(1,N,1));
indx2 =
[ 1]
[ 3]
[ 6]
[ 4]
[NaN]
What I have done here is to first initialise your output cells to have the same number of cells as your original data. Then I assign value 1, since that one will always be unique, it is the first entry. After that I use a for loop to first convert all four cell arrays (2 input, two output) to regular arrays for processing with ismember, where I check for the all non-equal number between the next input cell and the existing numbers in your output. Then find is employed to get the first non-matching number. Lastly, the numbers are assigned to the arrays if present.
As a comment on the usage of booleans with NaN, try NaN ~=NaN and NaN ==NaN. The first will give you 1, whilst the second will give you zero. This quality makes NaNs the ideal choice of filler here, because 0 == 0 will result in 1:
A = [1,2,5,4,NaN];
B = [1,3,7,NaN,NaN];
ismember(A,B)
=
1 0 0 0 0
Thus the NaNs do not equal one another and will therefore not pollute your solution.

input array of strings in matlab

Problem: Solve linear equations
I have a 3×3 matrix and I wanted to take 3 expressions as inputs which contain matrix cells like
2*b(1,1)+3*b(1,2)+3*b(1,3)
3*b(2,1)+4*b(2,3)+3*b(2,3)
and evaluate them with different cell values in matrix
0 1 0
1 0 0
1 0 0
0 1 0
0 1 0
1 0 0 etc.,
I used the following code, I got the result but I can only use the cell values. When I try to give expressions with numeral, it shows the following error:
*Warning: File: pro.m Line: 5 Column: 9 The expression on this line will generate an error when executed. The error will be: Error using
==> vertcat CAT arguments dimensions are not consistent.
??? Error using ==> pro at 5 Error using ==> vertcat CAT arguments dimensions are not consistent.*
Here is my code:
clc;
clear all;
close all;
cell = ['b(1,1)+b(1,2)';'b(2,1)+b(2 ,3)';'b(3,3)+b(3,2)'];
exp = cellstr(cell);
res = [0,0,0];
display(res);
display(exp);
a = zeros(3,3);
for i = 1:1:3
a(1,i) = 1;
if(i>1)
a(1,i-1) = 0;
end
for j = 1:1:3
a(2,j) = 1;
if(j>1)
a(2,j-1) = 0;
end
for k = 1:1:3
a(3,k) = 1;
if(k>1)
a(3,k-1) = 0;
end
b = a;
res(k) = eval(exp{k});
if res(1) == 1
if res(2) == 1
if res(3) == 1
display(res);
display(b);
break;
end
end
end
end
a(3,k)=0;
end
a(2,j) = 0;
end
;
Help how can I input strings with numerals and matrix cells...
This is not a valid expression to initialize a cell in Matlab:
cell = ['b(1,1)+b(1,2)';'b(2,1)+b(2 ,3)';'b(3,3)+b(3,2)'];
You have to use the curly brackets { }:
cell = {'b(1,1)+b(1,2)';'b(2,1)+b(2 ,3)';'b(3,3)+b(3,2)'};
BTW, if you want to solve linear equation of the form AX+b=0, you can simply try X=-inv(A)*b
% Define system
A = [2 3 1; 7 -1 1; 4 0 5];
b = [1 0 1].';
% Solve system
X = -inv(A)*b;

Matlab removing unwanted numbers from array

I have got a matlab script from net which generates even numbers from an inital value. this is the code.
n = [1 2 3 4 5 6];
iseven = [];
for i = 1: length(n);
if rem(n(i),2) == 0
iseven(i) = i;
else iseven(i) = 0;
end
end
iseven
and its results is this
iseven =
0 2 0 4 0 6
in the result i am getting both even numbers and zeros, is there any way i can remove the zeros and get a result like this
iseven =
2 4 6
To display only non-zero results, you can use nonzeros
iseven = [0 2 0 4 0 6]
nonzeros(iseven)
ans =
2 4 6
You can obtain such vector without the loop you have:
n(rem(n, 2)==0)
ans =
2 4 6
However, if you already have a vector with zeros and non-zeroz, uou can easily remove the zero entries using find:
iseven = iseven(find(iseven));
find is probably one of the most frequently used matlab functions. It returns the indices of non-zero entries in vectors and matrices:
% indices of non-zeros in the vector
idx = find(iseven);
You can use it for obtaining row/column indices for matrices if you use two output arguments:
% row/column indices of non-zero matrix entries
[i,j] = find(eye(10));
The code you downloaded seems to be a long-winded way of computing the range
2:2:length(n)
If you want to return only the even values in a vector called iseven try this expression:
iseven(rem(iseven,2)==0)
Finally, if you really want to remove 0s from an array, try this:
iseven = iseven(iseven~=0)
Add to the end of the vector whenever you find what you're looking for.
n = [1 2 3 4 5 6];
iseven = []; % has length 0
for i = 1: length(n);
if rem(n(i),2) == 0
iseven = [iseven i]; % append to the vector
end
end
iseven
To remove the all Zeros from the program we can use the following command,and the command is-
iseven(iseven==0)=[]

Resources