Matlab isn't incrementing my variable - matlab

I have the following matrix declared in Matlab:
EmployeeData =
1 20 100000 42 14
2 15 95000 35 14
3 18 70000 28 14
4 10 85000 35 14
5 10 40000 21 12
6 4 45000 14 8
7 3 50000 21 10
8 5 55000 21 14
9 1 25000 14 7
10 2 50000 21 9
42 4 100000 42 10
Where column 1 represents ID numbers, 2 represents years, 3 is salary, 4 is vacation days, and 5 is sick days. I am trying to find the maximum value of a column (in this case the salary column), and print out the ID associated with that value. If more than one employee has the maximum value, all the IDs with that maximum are supposed to be shown. So here is how I naively implemented a way to do it:
>> maxVal = [];
>> j = 1;
>> for i = EmployeeData(:, 3)
if i == max(EmployeeData(:, 3))
maxVal = [maxVal EmployeeData(j, 1)];
end
j = j + 1;
end
But it shows maxVal to be [] in my workspace variables, instead of [1 42] as I expected. Upon inserting a disp(i) in the for loop above the if to debug, I get the following output:
100000
95000
70000
85000
40000
45000
50000
55000
25000
50000
Just like I expected. But when I switch out that disp(i) with a disp(j), I get this for my output:
1
What am I doing wrong? Should this not work?

MATLAB for loops operate on rows, not columns. You should try replacing your for loop with:
for i = EmployeeData(:, 3)' % NOTE THE TRANSPOSE
...
end
EDIT: Note that you can do what you're trying to do without a forloop:
maxVal = EmployeeData(EmployeeData(:,3) == max(EmployeeData(:,3)),1);

Is this what you want?
>> EmployeeData(EmployeeData(:,3)==max(EmployeeData(:,3)),1)
ans =
1
42

Related

Short writing in Matlab for getting the minimum of two vectors at each position

I have the following code:
for i=1:length(Z1)
if Z1(i) < Z2(i)
min_vec(i) = Z1(i)
else
min_vec(i) = Z2(i)
end
end
I am wondering if there is a more "matlab" short writing way to achieve the same goal?
You can use min function:
If you are only interest only to the minimum value, you can simply use Z1 and Z2 as input:
min_Z1_Z2=min(Z1,Z2)
if you are also interested to know in which of the two array the minimun value is, you can call min with two parameters as output and passing as input the two array as a matrix
[min_val,Z1_Z2]=min([Z1;Z2])
In min_val you have the minimum value, in Z1_Z2 the values 1 or 2 according if the min value is in the first or the second row, that is Z1 or Z2
Z1=randi([1 33],1,10)
Z2=randi([1 33],1,10)
min_Z1_Z2=min(Z1,Z2)
[min_val,Z1_Z2]=min([Z1;Z2])
>> Z1
Z1 =
24 12 16 21 22 15 28 7 17 29
>> Z2
Z2 =
27 26 23 2 5 10 25 23 29 9
>> min_val
min_val =
24 12 16 2 5 10 25 7 17 9
>> Z1_Z2
Z1_Z2 =
1 1 1 2 2 2 2 1 1 2

Combination of arrays in 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

"Find and replace" from a matrix MATLAB

I have a matrix that is like the following:
a = [10 0; 12 5; 10 0; 12 0; 15 0; 15 2];
a =
10 0
12 5
10 0
12 0
15 0
15 2
I am looking to create a new matrix which find and replaces the zeros with a value that is dependent on the first column's value. The key is this matrix:
Key =
10 100
12 200
15 300
If the value is already greater than zero in the first column I would like to leave it. The output would look like this:
Output =
10 100
12 5
10 100
12 200
15 300
15 2
You can do it in one line using logical indexing smartly:
a(~a(:,2),2)=arrayfun(#(x)Key(Key(:,1)==x,2),a(~a(:,2),1))
%a =
% 10 100
% 12 5
% 10 100
% 12 200
% 15 300
% 15 2

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'