Combination of arrays in Matlab - matlab

I have a problem on a part of a program and I would appreciate some help.
My main objective is to use all possible pairs in two arrays. With some help i managed to get this
A = nchoosek(0:15, 2)
arr1 = A(:,1);
arr2 = A(:,2);
Result = arr1.*arr2 + arr1.^2 + arr2.^2;
I want to use all the combinations in arr1 and arr2 to solve the result equation and print out the result like this:
arr1 arr2 Result
0 0 0
1 1 3
2 0 4
and so on.. but not all the combinations are used when I try this approach. What should I do to get all the possible combinations?

Matlab has meshgrid function to eliminate loops for this purpose, for example
>> a1=[1:4];
>> a2=[0:3];
>> [x1,x2]=meshgrid(a1,a2);
>> r=x1.*x2+x1.^2+x2.^2;
or to use square once
>> r1=(x1+x2).^2-x1.*x2;
UPDATE: for your case you use 0:15 values, using them will result with
>> a1=[0:15];a2=[0:15];
>> [x1,x2]=meshgrid(a1,a2);
>> r=-x1.*x2+(x1+x2).^2;
>> size(r)
ans =
16 16
UPDATE 2 Note that your method doesn't create all pairs, for example (0,0) or (1,1) won't be there also only of one of the (x,y) (y,x) pairs will be there for x!=y values. Other than double loops the preferred approach is what I proposed. You can gather the results in a matrix in the form you want easily as well
>> n=size(r,1);
>> R=[reshape(x1,1,n*n); reshape(x2,1,n*n); reshape(r,1,n*n)]'
R =
0 0 0
0 1 1
0 2 4
0 3 9
0 4 16
0 5 25
0 6 36
0 7 49
...
15 6 351
15 7 379
15 8 409
15 9 441
15 10 475
15 11 511
15 12 549
15 13 589
15 14 631
15 15 675

Related

Convolution of two symbolic arrays on Matlab

I have two arrays:
p1=[sym(1) sym(2)]
p2=[sym(3) sym(4)]
I want to do the convolution of those two lists using conv function.
Matlab outputs the following:
Error using conv2
Invalid data type. First and second arguments must be numeric or logical.
Error in conv (line 43)
c = conv2(a(:),b(:),shape);
Can anyone help me how to deal with that?
Edit 1: i have not symbolic math toolbox so i demonstrated a matrix-wise operation on numeric values to calculate conv, i think this should do the trick on symbolic values either.
the conv function just accept numeric values.
there is a way to calculate conv matrix-wise
i demonstrate it with an example:
assume u and v are as follows :
u =
1 2 1 3
v =
2 7 1
>> conv(u,v)
ans =
2 11 17 15 22 3
instead we could first calculate u'*v, then do some rearranging and summing to calculate conv:
so first :
>> c=u'*v
c=
2 7 1
4 14 2
2 7 1
6 21 3
then we do some rearranging:
>> d=[c;zeros(3,3)]
d =
2 7 1
4 14 2
2 7 1
6 21 3
0 0 0
0 0 0
0 0 0
>>e= reshape(d(1:end-3),[6,3])
e=
2 0 0
4 7 0
2 14 1
6 7 2
0 21 1
0 0 3
and finally adding values together :
>> sum(e,2)
ans =
2
11
17
15
22
3
you can write your own code to use "v size" to do it(add (numel(v)*numel(v)) zeros to end of u'*v and so on.)

Comparing two matrix and took the value if match the criteria

I have some question. I have 2 matrix, it's have same size.
For example, first matrix :
1
1
0
0
1
0
Second matrix
34
56
12
12
33
14
Then, I want to compare this two matrix and groups it by the criteria on first matrix
so I will have this two groups matrix :
Matrix when the first matrix is have value 1
34
56
33
and
Matrix when the first matrix is have value 0
12
12
14
You could try this:
a = [1 1 0 0 1 0]';
b = [34 56 12 12 33 14]';
b(a==1)
b(a==0)

Converting Single dimensional matrix to two dimension in Matlab

Well I do not know if I used the exact term. I tried to find an answer on the net.
Here is what i need:
I have a matix
a = 1 4 7
2 5 8
3 6 9
If I do a(4) the value is 4. So it is reading first column top to buttom then continuing to next .... I don't know why. However,
What I need is to call it using two indices. As row and column:
a(1,2)= 4
or even better if i can call it in the following way:
a{1}(2)=4
What is this process really called (want to learn) and how to perform in matlab.
I thought of a loop. Is there a built in function
Thanks a lot
Check this:
a =
18 18 16 18 18 18 16 0 0 0
16 16 18 0 18 16 0 18 18 16
18 0 18 18 0 16 0 0 0 18
18 0 18 18 16 0 16 0 18 18
>> a(4)
ans =
18
>> a(5)
ans =
18
>> a(10)
ans =
18
I tried reshape. it is reshaping not converting into 2 indeces
If you've already got a matrix, you already can access it with two indices:
if you've got
a = 1 4 7
2 5 8
3 6 9
you can access it as
a(3,2) = 6
However, the indexing goes from the top left, row first then column. If you want to get at the "4" in the matrix then do:
a(1,2)
To reshape a vector/matrix/array, use reshape().
Or you could leave it as a one dimensional array and just use
((Column - 1) * 3) + Row - 1) as the index. 3 because there are three columns.
NB a(4) = 4 because of the way you've arranged columns and rows in the one dimensional array, yours is "loaded" as
R1C1,R2C1,R3C1, R1C2 etc wher R is row and C is column
If that's inconvenient then you just need to get whatever fills the array row then column so the above mapping would be
((Row - 1) * 3) + Column - 1)
Don't do Matlab so above code assumes array starts at 0, if not just add 1 to it.

Matlab combine matrix

student1 student2 student3
code score code score code score
1 20 1 100 1 22
2 11 3 11 2 90
3 12 4 22 5 11
4 11
5 28
This question is related to How do I combine uneven matrices into a single matrix? but a little bit different. I want to combine n files which have different size. Each file read through loop. How I can get the output as shown below?
for i=1:n
....
inputdata=[code score];
sortdata= sortrows(inputdata,1);
end
Output
code s1 s2 s3
1 20 100 22
2 11 0 90
3 12 11 0
4 11 22 0
5 28 0 11
Instead of
inputdata=[code score];
sortdata = sortrows(inputdata,1);
use
completedata(code, n+1) = score;
This way you are using code as index to your final array. Initialising completedata before the loop would probably be a good idea.
completedata = [(1:codemax)', zeros(codemax, n)];

Comparing content of two columns in MATLAB

I need to compare the content of two tables, more exactly two columns (one column per table), in MATLAB to see for each element of the first column, if there is an equal element in the second column.
Should I use a for loop or is there an existing MATLAB function that does this?
If the order is important, you do element-wise comparison, after which you use all
%# create two arrays
A = [1,2;1,3;2,5;3,3];
B = [2,2;1,3;1,5;3,3];
%# compare the second column of A and B, and check if the comparison is `true` for all elements
all(A(:,2)==B(:,2))
ans =
1
If the order is unimportant and all elements are unique, use ismember
all(ismember(A(:,1),B(:,1))
ans =
1
If the order is unimportant, and there are repetitions, use sort
all(sort(A(:,1))==sort(B(:,2)))
ans =
0
did you know you could do this:
>> a = [1:5];
>> b = [5:-1:1];
>> a == b
ans =
0 0 1 0 0
so you could compare 2 columns in matlab by using the == operator on the whole column. And you could use the result from that as a index specifier to get the equal values. Like this:
>> a(a == b)
ans =
3
This mean, select all the elements out of a for which a == b is true.
For example you could also select all the elements out of a which are larger than 3:
>> a(a > 3)
ans =
4 5
Using this knowledge I would say you could solve your problem.
For arithmetic values, both solutions mentioned will work. For strings or cell arrays of strings, use strcmp/strcmpi.
From the help file:
TF = strcmp(C1, C2) compares each element of C1 to the same element in C2, where C1 and C2 are equal-size cell arrays of strings. Input C1 or C2 can also be a character array with the right number of rows. The function returns TF, a logical array that is the same size as C1 and C2, and contains logical 1 (true) for those elements of C1 and C2 that are a match, and logical 0 (false) for those elements that are not.
An example (also from the help file):
Example 2
Create 3 cell arrays of strings:
A = {'MATLAB','SIMULINK';'Toolboxes','The MathWorks'};
B = {'Handle Graphics','Real Time Workshop';'Toolboxes','The MathWorks'};
C = {'handle graphics','Signal Processing';' Toolboxes', 'The MATHWORKS'};
Compare cell arrays A and B with sensitivity to case:
strcmp(A, B)
ans =
0 0
1 1
Compare cell arrays B and C without sensitivity to case. Note that 'Toolboxes' doesn't match because of the leading space characters in C{2,1} that do not appear in B{2,1}:
strcmpi(B, C)
ans =
1 0
0 1
To get a single return value rather than an array of logical values, use the all function as explained by Jonas.
You can use for loop (code below) to compare the content of the column 1 and column % 2 in the same table:
clc
d=[ 19 24 16 12 35 0
16 16 18 0 23 18
16 10 7 10 13 24
19 8 30 0 12 26
16 12 4 1 13 12
24 0 31 0 40 0
12 11 10 6 20 0
16 11 6 2 25 9
17 9 21 0 17 8
20 0 7 10 22 0
13 16 12 18 17 13
17 23 17 0 23 20
25 0 10 3 17 15
14 4 4 17 12 10
19 24 21 5 35 0
15 20 5 0 10 31
13 8 0 16 40 0
18 27 26 1 19 14
12 0 2 0 12 4
20 0 6 2 15 21
20 0 26 0 18 26
12 11 1 13 19 15
14 0 20 0 9 16
14 15 6 12 40 0
20 0 8 10 18 12
10 11 14 0 13 11
5 0 22 0 8 12 ];
x1=d(:,1);
y1=d(:,2);
for i=1:27
if x1(i)>y1(i);
z(i)=x1(i);
else
z(i)=y1(i);
end
end
z'