Matlab Array removing zero value - arrays

I am new to MatLab and programming in general. I have been set the following problem:
4 players take part in a competition each person starts with 100 points.
Each person randomly plays another player.
If a player wins they get 1 point and the losing player loses 1 point from their total.
When a player has zero points they are elimated.
The game is over when there is only 1 player left.
I assume I need to create some sort of matrix array with 4 players and their 4 scores
A = [1 2 3 4; 100 100 100 100]
Is this correct and how would i remove a player and their score once it reaches zero.
Any help would be greatly recieved. Thanks

I think you are on the right track, assuming you define A as such, you can remove the players with nonpositive score as such:
A = [1 2 3 4; 100 100 0 100]
idx = A(2,:)<0.01 %To prevent suffering from rounding errors
A = A(:,~idx)

I just came across this page while looking for this answer myself (I'm also new to Matlab).
However, the above answers seem too complicated; Matlab's matrix addressing and assignment operators solve this problem very simply, as I found out after a bit of experimentation:
A = [1 2 3 4; 100 0 100 100]
A =
1 2 3 4
100 0 100 100
A(:,A(2,:)==0)=[]
A =
1 3 4
100 100 100
I hope this is useful to someone.

The 1st answer is absolutely correct. However, in order to approach this problem, there is a straight-forward syntax built for this purpose, using sparse you will also be able to extract the player with 0-points directly:
A = [1 2 3 4; 100 100 0 100]
ans= sparse (A(2,:))
Good luck ;)

Related

How to plot a 2D plot in MATlab from Three Matrices and an array?

I am trying to plot a figure using three matrices but somehow I couldn't understand. I have three matrices and an array. Suppose,
A =
1 2 3
4 5 4
7 8 9
B =
2 3 13
5 11 10
9 7 6
C =
1 2 3
2 3 13
5 11 10
and an array
Y= [0.001 0.0002 0.0004].
Now I want to plot it in such a way that array values should be on y axis while against 0.001, 0.002 and 0.0004 the matrices value should be arranged.
for examples, the y=0.001, A(1,1)=1, y=0.0002, B(1,1)=2 y=0.0004, C(1,1)=1 for a single line.
and similarly process goes for A(i,j),B(i,j) and c(i,j) points using loop to plot all lines on a single figure.
Thanks
So, the first plot is plot([1,2,1],Y), the next one is plot([2,3,2],Y) and so on?
If so, you could do it like that
X = cat(3,cat(3,A,B),C);
X = reshape(permute(X,[3,1,2]),3,9);
plot(X,Y,'--x');
which gives a plot like this:
Is this what you were looking for? If not, I didn't understand your question well and I'd like to ask you to rephrase it.
You can create a 3D array (tensor) and access it in a loop.
T(:,:,1) = A;
T(:,:,2) = B;
T(:,:,3) = C;
figure;
for idi = 1:size(A,1)
for idj = 1:size(A,2)
plot(squeeze(T(idi,idj,:)).',Y); hold on;
end
end
Accessing the third dimension is not fastest operation (as they are not store sequential in memory) and if the matrices are larger you might consider reshape.
I did not understand you wanted the vector Y to be on the x-axis or y-axis (and neither of those plots make sense to me) but I am sure you can modify the code from here for your needs.

Keeping the same order in arrays - Brain Teaser?

* I need the output to be a matrix because a much larger part of the code needs the output of this code to be a matrix for it's input*
Hello. I have a question related to matrix manipulation.
I need to keep the order the same in the matrix. Please see below for what I am trying to do.
lets say I start out with 3 fruits:
fruits = {'apple','orange','berry'};
and the amounts of each fruit:
amount = [3,5,2]
then the next day the amount changes:
amount = [2,4,3]
so now, my matrix will be:
3 5 2
2 4 3
but what if the next day I needed to add another fruit:
fruits = {'apple','orange','berry','banana'};
and the amounts are:
amount = [3,4,2,1]
how do I make my new matrix like this:
3 5 2 NaN
2 4 3 NaN
3 4 2 1
then on the next day, I was not given 1 of the original fruits:
fruits = {'apple','berry','banana'};
and the amount would be:
amount = [5,1,4]
then I need the matrix to be like the following:
3 5 2 NaN
2 4 3 NaN
3 4 2 1
5 NaN 1 4
How would I write the code for it to be able to handle all of these situations?
use structures then convert struct2cell and convert empties to NaNs

Given 2 positions (x1,y1) and (x2,y2) print the sum of all elements within the area of rectangle in O(1) time

A 2D matrix is given to you. Now user will give 2 positions (x1,y1) and (x2,y2),
which is basically the upper left and lower right coordinate of a rectangle formed within the matrix.
You have to print sum of all the elements within the area of rectangle in O(1) running time.
Now you can do any pre computation with the matrix.
But when it is done you should answer all your queries in constant time.
Example : consider this 2D matrix
1 3 5 1 8
8 3 5 3 7
6 3 9 6 0
Now consider 2 points given by user (0, 2) and (2, 4).
Your solution should print: 44.
i.e., the enclosed area is
5 1 8
5 3 7
9 6 0
Since your question seems to be related to homeworks, i am just posting a clue... It is a formula that may inspire you :
What does this sum of terms represent ?
Rewrite your problem using mathematical tools, indexes like i and j ...
maybe this can also help:
courtosy of: http://www.techiedelight.com/calculate-sum-elements-sub-matrix-constant-time/

Breadth-first Search Exercise - AI

I'm new student of AI and I'm trying to do some exercises before I start programming to understand the logic. However, I'm having a hard time doing the exercises, I want to know if someone can help me with this one (any explanation, where I can find material which can helps are welcome):
Consider Deep Blue can evaluate 200 million positions a
second. Assume at each move, a pawn can go to 2 possible
positions, a rook 14, a knight 8, a bishop 14, a queen 28,
and a king 8. Each side has 8 pawns, 2 rooks, 2 knights, 2
bishops, a queen and a king. Under standard regulations,
each side makes 40 moves within the first 2 hours (or 3
minutes a move on the average)
a) Using the breadth-first search algorithm, how many
levels can Deep Blue evaluate (visit) before each move
(in 3 minutes)?
b) To examine 20 levels in 3 minutes, how many positions Deep Blue needs to evaluate (visit) in a second?
I really appreciate any help. Thanks a lot guys.
Basically, you multiply the number of pieces with their individual potential mobility to get the theoretical branching factor for one side. That is, the number of possible moves at each search level.
Then you raise that figure to the power of the search depth to get the number of total positions to evaluate.
So if for the first search ply (half-move), the branching factor is N, then for a two-ply search the total number of positions is N*N, for three it's N*N*N, and so on.
I'll leave the rest up to you :)
`I don't know if I'm right, but this was my answer for question b):
p = 2 x 8 = 16
r = 14 x 2 = 28
k = 8 x 2 = 16
b = 14 x 2 = 28
q = 28 x 1 = 28
k = 8 x 1 = 8
Total = 124 x 2 = 248 x 20 = 4960 position p/ level
1 min = 60 x 3 = 180 seconds
4960/180 = 25.7~ => 28 per seconds`

MATLAB: Return the largest number in an array?

I'm new to MATLAB (and this website!) and I needed some help with a problem I had been assigned for class. I searched this website for similar MATLAB problems, but I didn't come across any. The problem is asking the user to return the biggest number which is next to a zero. In other words, write a function which takes a list/array of numbers as input and returns the largest number which is adjacent to a zero. For instance, if
a=[1 -2 3 4 0 5 6 0 -7], Output: y=6.
I tried to solve the problem using a somewhat complex function I found online, and it seems to work on MATLAB. However, it won't work on our automated online MATLAB grading system as the command "imdilate" isn't recognized:
x=[1 2 0 4 5 -6 0 7 0 8]
zero_mask = (x == 0);
adjacent_to_zero_mask = imdilate(zero_mask, [1 0 1]);
max_value_adjacent_to_zero = max(x(adjacent_to_zero_mask));
y=max_value_adjacent_to_zero
I wanted to ask, is there is much simpler method of solving this problem not involving "imdilate" or other similar functions?
Thank you for your help, I really appreciate it!
I came up with a dirty solution:
a=[0 1 -2 3 4 0 5 6 0 -7];
I=find(a==0);
I=unique([I+1,I-1]);
I=I((I>0)&(I<=length(a)));
output = max(a(I));

Resources