I have made this data.txt file and imported it into matlab using import wizard.
this is the result :
This table is my data base and I want to search into it column wise (each variable). I used strread as this lets me to search columns, for example :
var1=strread(VarName12(1,1),'%d',"delimiter','|')
I wrote several lines of code using strread in a script and then run it. when I check the workspace var1 and other variables that included strread result are missing and typing them in command window gives me this : Undefined function or variable 'End'. I typed sttread in command window and used variables in data.txt and got the result but it doesn't work when I run my script. I can't understand what's the problem? ( I'm using matlab R2013a and there is warning that says : using strread is not recommende. use TEXTSCAN instead. I can't do the same with textscan also strread works properly when written in workspace ). I don't know what is the problem. can give a clue?
EDIT :
this is the code
(Note: I'm writing a "biometric recognition system based on ear" and the database is data extracted from images1 to 5). This is the classification section of the code:
Nbcoordinates = vertcat(BEcell{:,1});
Necoordinates = vertcat(BEcell{:,2});
import DATA.txt.*; % import our data set
for i=1:5
token1 = 0; token2 = 0; token3 = 0; token4 = 0; token5 = 0; token6 = 0; token7 = 0;
if(VarName1(i, 1) == V{1,1}(1,2) && VarName3(i, 1) == V{1,2}(1,2) && VarName5(i,1) == V{1,3}(1,2) && VarName2(i,1) == V{1,1}(1,3) && VarName4(i,1) == V{1,2}(1,3) && VarName6(i,1) == V{1,3}(1,3))
% Check number of endings for each component
numberofendings = strread(VarName10{i,1}, '%d', 'delimiter', '|');
for j=1:size(Ne,1)
if(numberofendings(j,1)~=Ne(j,1))
break;
end
end
if(j >= size(Ne,1))
token1 = 1;
end
% check number of bifurcations for each components
numberofbifurcations = strread(VarName11{i,1}, '%d', 'delimiter', '|');
for j=1:size(Nb,1)
if(numberofbifurcations(j,1) ~= Nb(j,1))
break;
end
end
if(j >= Nb)
token2 = 1;
end
% Check Intersections1,2,3 Cordinates
Intercoordinate1 = strread(VarName7{i,1}, '%f', 'delimiter', '|');
m = 1;
for j=1:NI1
if((Intersection1(j, 1) ~= Intercoordinate1(m,1)) || (Intersection1(j, 2) ~= Intercoordinate1(m+1,1)))
break;
end
m = m + 2;
end
if(j >= NI1)
token3 = 1;
end
Intercoordinate2 = strread(VarName8{i,1}, '%f', 'delimiter', '|');
m = 1;
for j=1:NI2
if((Intersection2(j, 1) ~= Intercoordinate2(m,1)) || (Intersection2(j, 2) ~= Intercoordinate2(m+1,1)))
break;
end
m = m + 2;
end
if(j >= NI2)
token4 = 1;
end
Intercoordinate3 = strread(VarName9{i,1}, '%s', 'delimiter', '|');
m = 1;
for j=1:NI3
bi1 = cell2mat(Intercoordinate3(m, 1));
bi2 = cell2mat(Intercoordinate3(m + 1, 1));
if((Intersection3(j, 1) ~= str2num(bi2)) || (Intersection3(j, 2) ~= str2num(bi2)))
break;
end
m = m + 2;
end
if(j >= NI3)
token5 = 1;
end
% Check endings coordinate of each component
Endcoor = strread(VarName12{i,1}, '%d', 'delimiter', '|');
m = 1;
for j=1:sum(Ne)
en1 = Endcoor(m, 1);
en2 = Endcoor(m + 1, 1);
if((Necoordinates(j, 1) ~= en1) || (Necoordinates(j, 2) ~= en2))
break;
end
m = m + 2;
end
if(j >= sum(Ne))
token6 = 1;
end
% check bifurcation coordinates of each component
Bifcoor = strread(VarName13{i,1}, '%d', 'delimiter', '|');
m = 1;
for j=1:sum(Nb)
en1 = Bifcoor(m, 1);
en2 = Bifcoor(m + 1, 1);
if((Nbcoordinates(j, 1) ~= en1) || (Nbcoordinates(j, 2) ~= en2))
break;
end
m = m + 2;
end
if(j >= sum(Nb))
token7 = 1;
end
if(token1 ==1 && token2 == 1 && token3== 1 && token4 == 1 && token5 == 1 && token6== 1 && token7==1 )
disp(['This image is a member of Class ',num2str(i)]);
break;
end
end
end
if(token1 == 0 || token2 == 0 || token3 == 0 || token4 == 0 || token5 == 0 || token6== 0 || token7==0)
disp('Doesn;t exist in the data base ');
end
end
Related
I have created an array full of letters and I'm stuck on implementing the function for the cell to randomly move in one of 8 directions (N, NE, E, SE, S, SW, W, NW). I've used a switch statement for the basic 4 direction but couldn't figure out the other 4 directions.
void randomStep()
{
if ((island[ro][co + 1] != ('B'||'L') || co == NUMROWS - 1 )&& (island[ro + 1][co] != ('B'||'L') || ro == NUMCOLS -1) && (island[ro - 1][co] != ('B'||'L') || ro == 0)
&& (island[ro][co - 1] != ('B'||'L') || co == 0))
break;
int direction = rand() % 8;
switch (direction) {
case 0: if (co < NUMROWS - 1 && island[ro][co + 1] == 'B'||'L'){ //move right
co++;
break;
}
case 1: if (ro < NUMCOLS -1 && island[co + 1][ro] == 'B'||'L') { //move down
ro++;
break;
}
case 2: if (ro > 0 && island[ro - 1][co] == 'B'||'L'){ //move up
ro--;
break;
}
case 3: if (co > 0 && island[ro][co - 1] == 'B'||'L') { //move left
co--;
break;
}
You can simply combine the conditions and results of two other cases. Here is an example for NW (left and up)
case 4: if (ro > 0 && co > 0 && island[ro - 1][co - 1] == 'B'||'L') { //move up & left
ro--;
co--;
}
break;
Note that I have moved the position of the break; to be outside the if code block.
I think you may also have an error with
... island[ro - 1][co - 1] == 'B'||'L' ...
which I guess should be
... island[ro - 1][co - 1] == 'B' || island[ro - 1][co - 1] == 'L' ...
and similarly in the other cases.
You can move parts of your code into functions:
bool mayGoUp(int ro, int co)
{
return ro > 0;
}
bool mayGoDown(int ro, int co)
{
return ro < NUMROWS - 1;
}
bool mayGoLeft(int ro, int co)
{
return co > 0;
}
bool mayGoRight(int ro, int co)
{
return co < NUMCOLS - 1;
}
Note: I changed the logic a bit: from co < NUMROWS - 1 to co < NUMCOLS - 1; not sure which one is the correct one.
Then you can combine them in a straightforward way:
bool mayGoUpLeft(int ro, int co)
{
return mayGoUp(ro, co) && mayGoLeft(ro, co);
}
Then, using them in your code will make your code clearer:
switch (direction) {
case 0:
if (MayGoRight(ro, co) && island[ro][co + 1] == ...
{ //move right
co++;
break;
}
...
case 99:
if (MayGoUpRight(ro, co) && ...
{ // move up and right
ro--;
co++;
break;
}
This is the verification from a connect four game prototype, but it seems I've done something wrong.
I want that everytime the player is making a move, the function will verify if there he won or not, by verifying vertically, horizontally, and eventually, on the diagonal.
But it seems that it does not verify correctly, because in some cases, even though there are only 2 moves made, the functions returns 1.
int verifyGame(int gamePosition, int gameVariable, char gameArray[HEIGTH][WIDTH])
{
if(gameArray[gamePosition][gameVariable] == gameArray[gamePosition + 1][gameVariable] == gameArray[gamePosition + 2][gameVariable] == gameArray[gamePosition + 3][gameVariable]) //verify vertically
return 1;
else
if(gameArray[gamePosition][gameVariable] == gameArray[gamePosition][gameVariable - 3] == gameArray[gamePosition][gameVariable - 2] == gameArray[gamePosition][gameVariable - 1]) //verify horizontally
return 1;
else
if(gameArray[gamePosition][gameVariable] == gameArray[gamePosition][gameVariable - 2] == gameArray[gamePosition][gameVariable - 1] == gameArray[gamePosition][gameVariable + 1])
return 1;
else
if(gameArray[gamePosition][gameVariable] == gameArray[gamePosition][gameVariable - 1] == gameArray[gamePosition][gameVariable + 1] == gameArray[gamePosition][gameVariable + 2])
return 1;
else
if(gameArray[gamePosition][gameVariable] == gameArray[gamePosition][gameVariable + 1] == gameArray[gamePosition][gameVariable+ 2] == gameArray[gamePosition][gameVariable + 3])
return 1;
//verify diagonally
else return 0;
};
This is where the function is called. The switch verifies the users input, and then it places the value in the matrix, and then verifies for won
printf("playerPick is : %d\n", playerPick);
fflush(stdout);
switch(playerPick)
{
case 1:
if(gameVariables[0] >0 && gameVariables[0] < 7)
{
--gameVariables[0];
gameArray[gameVariables[0]][0] = (char) 82;
ifWon = verifyGame(gameVariables[0], 0, gameArray);
}
printArray(gameArray);
break;
case 2:
if(gameVariables[1] >0 && gameVariables[1] < 7)
{
--gameVariables[1];
gameArray[gameVariables[1]][1] = (char) 82;
ifWon = verifyGame(gameVariables[1], 1, gameArray);
}
printArray(gameArray);
break;
case 3:
if(gameVariables[2] >0 && gameVariables[2] < 7)
{
--gameVariables[2];
gameArray[gameVariables[2]][2] = (char) 82;
ifWon = verifyGame(gameVariables[2], 2, gameArray);
}
printArray(gameArray);
break;
case 4:
if(gameVariables[3] >0 && gameVariables[3] < 7)
{
--gameVariables[3];
gameArray[gameVariables[3]][3] = (char) 82;
ifWon = verifyGame(gameVariables[3], 3, gameArray);
}
printArray(gameArray);
break;
case 5:
if(gameVariables[4] >0 && gameVariables[4] < 7)
{
--gameVariables[4];
gameArray[gameVariables[4]][4] = (char) 82;
ifWon = verifyGame(gameVariables[4], 4, gameArray);
}
printArray(gameArray);
break;
case 6:
if(gameVariables[5] >0 && gameVariables[5] < 7)
{
--gameVariables[5];
gameArray[gameVariables[5]][5] = (char) 82;
ifWon = verifyGame(gameVariables[5], 5, gameArray);
}
printArray(gameArray);
break;
case 7:
if(gameVariables[6] >0 && gameVariables[6] < 7)
{
--gameVariables[6];
gameArray[gameVariables[6]][6] = (char) 82;
ifWon = verifyGame(gameVariables[6], 6, gameArray);
}
printArray(gameArray);
break;
}
printf("%d %d %d %d %d %d %d\n", gameVariables[0], gameVariables[1], gameVariables[2], gameVariables[3], gameVariables[4], gameVariables[5], gameVariables[6]);
printf("ifwon : %d\n", ifWon);
#Weather Vane's answer is correct. The logic used in your original post is not correct for a verification.
One reason you may not have caught it yourself may be the complicated way it was written. Try simplifying the user input verification code: (Range checking the user input values is all that is necessary.)
//User input range checking:
if((gamePosition >= x)&& //where `x` is minimum for gamePosition
(gamePosition <= y)&& //where `y` is maximum for gamePosition
(gameVariable >= z)&& //where `z` is minimum for gameVariable
(gameVariable <= w)) //where `w` is maximum for gameVariable
{//continue }
else
{
printf("Invalid value. Please re-enter");
return -1;
}
Another opportunity for simplification is to note that each of your case statements contain identical code, with the exception of the value of the case. Because of this the entire switch(...){...} can be replaced with a single if statement:
//assuming playerPick >= 1
if(gameVariables[playerPick-1] >0 && gameVariables[playerPick-1] < 7)
{
--gameVariables[playerPick-1];
gameArray[gameVariables[playerPick-1]][playerPick-1] = (char) 82;
ifWon = verifyGame(gameVariables[playerPick-1], playerPick-1, gameArray);
}
printArray(gameArray);
Also note that although the statement:
gameArray[gameVariables[0][0] = (char) 82; //what is 82?
is perfectly legal, the variable gameArray[0][0] is just a char, so casting the value 82 is not necessary. Also, C syntax provides a way to pull out the ASCII decimal value of the character by surrounding it with the graves symbol, allowing the following form, which is more readable:
gameArray[gameVariables[0]][0] = `R`; //intuitive
You cannot chain equality testing as you are attempting. The code will execute, but not as you suppose. Your code
if(gameArray[gamePosition][gameVariable] ==
gameArray[gamePosition + 1][gameVariable] ==
gameArray[gamePosition + 2][gameVariable] ==
gameArray[gamePosition + 3][gameVariable])
must be split up into individual tests, such as:
if(gameArray[gamePosition][gameVariable] == gameArray[gamePosition + 1][gameVariable] &&
gameArray[gamePosition][gameVariable] == gameArray[gamePosition + 2][gameVariable] &&
gameArray[gamePosition][gameVariable] == gameArray[gamePosition + 3][gameVariable])
and on the other lines too.
I'm doing homework for UNI and I got to do a Tic-Tac-Toe without any decision taken by player, the moves are all chosen randomly. So if the character on matrix is ' ' it means it's free, while if it's 'X' or 'O' it should generate another move. This is the code (language C):
if (playerTurn == 1){
playerSymb = 'X';
}
else if (playerTurn == 2){
playerSymb = 'O';
}
if (matrix[rand1][rand2] == ' '){
matrix[rand1][rand2] = playerSymb;
} else if(matrix[rand1][rand2] == 'X' || matrix[rand1][rand2] == 'O'){
do{
randAlt1 = MINRND + rand()%(MAXRND - MINRND +1);
randAlt2 = MINRND + rand()%(MAXRND - MINRND +1);
}while (matrix[randAlt1][randAlt2] != 'X' && matrix[randAlt1][randAlt2] != 'O');
matrix[randAlt1][randAlt2] = playerSymb;
}
I did not copied the whole code because it's not finished at all, i just need help solving this. But if I try to run this, the Symbols can be overwritten, like if I have a 'X' at matrix[1][2], it's possible that it will be a 'O' after some turns. So how can I make moves do not overwrite? (sorry for bad english).
Just put correct condition:
while (matrix[randAlt1][randAlt2] == 'X' || matrix[randAlt1][randAlt2] == 'O')
(i.e. try again if this cell is not empty)
Also it is easy to simplify your code without loosing of anything:
randAlt1 = rand1;
randAlt2 = rand2;
while (matrix[randAlt1][randAlt2] != ' ') {
randAlt1 = MINRND + rand()%(MAXRND - MINRND +1);
randAlt2 = MINRND + rand()%(MAXRND - MINRND +1);
}
matrix[randAlt1][randAlt2] = (playerTurn == 1) ? 'X' : 'O';
And it is better to add loop guard to prevent infinite loop (or to add special checks for this case):
randAlt1 = rand1;
randAlt2 = rand2;
int nbAttempts = 0;
while (matrix[randAlt1][randAlt2] != ' ' && nbAttempts < 100) {
randAlt1 = MINRND + rand()%(MAXRND - MINRND +1);
randAlt2 = MINRND + rand()%(MAXRND - MINRND +1);
nbAttempts++;
}
if (matrix[randAlt1][randAlt2] != ' ') {
// show error message and stop the game
}
matrix[randAlt1][randAlt2] = (playerTurn == 1) ? 'X' : 'O';
You choose an arbitrary position and then test if it is free – possibly multiple times. But you can also choose a number of a free position and then find it.
First set up a turn counter
int turnNo = 0;
then make a loop for alternate moves, which chooses one of 9-turnNo unused positions, finds it, marks is with a player mark and tests if the move made a line of three:
while(turnNo < 9)
{
char currPlayerMark = ...choose 'X' or 'O';
int freePos = 9 - turnNo;
int currPos = rand() % freePos; // 0 .. freePos-1
for(x=0; x<3; x++)
{
for(y=0; y<3; y++)
{
if(matrix[x][y] == ' ') // a free position
if(--currPos < 0) // the sought one
break; // break the inner loop
}
if(currPos < 0)
break; // break the outer loop
}
matrix[x][y] = currPlayerMark;
if(test_for_win_position(x,y))
{
message_a_win_of_player(currPlayerMark);
break; // turnNo < 9 here
}
turnNo ++;
}
Finally test if the loop terminated with no 'win':
if(turnNo == 9)
message_its_a_draw(); // no-one wins
A function to test the win position might look like this:
int test_for_win_position(int x, int y)
{
char mark = matrix[x][y];
// check a column
if(matrix[x][0] == mark && matrix[x][1] == mark && matrix[x][2] == mark)
return 1;
// check a row
if(matrix[0][y] == mark && matrix[1][y] == mark && matrix[2][y] == mark)
return 1;
// check one diagonal
if(x==y)
if(matrix[0][0] == mark && matrix[1][1] == mark && matrix[2][2] == mark)
return 1;
// check another diagonal
if(x+y==2)
if(matrix[0][2] == mark && matrix[1][1] == mark && matrix[2][0] == mark)
return 1;
// current player has not won (yet)
return 0;
}
I need to write a program that returns the largest number entered by a user. The user can enter as many numbers >= 0 as they want and when they enter a -1, the program will stop and return the largest number. I am really struggling to do this. So far, I have this:
validInput = false;
while (~validInput)
fprintf('Enter a number >= 0 or a negative to quit.\n');
num = input('Enter a number or -1 to quit: ');
if(num == -1)
validinput = true;
counter = 0;
elseif(num>=0)
counter = counter+1;
end;
if(counter == 0)
fprintf('No values entered!');
else
array = (counter);
m = max(counter);
disp(m);
end
end``
enteredNumber = 0;
biggestNumber = 0;
while (enteredNumber ~= -1)
enteredNumber = input('Enter a number : ');
if enteredNumber > biggestNumber
biggestNumber = enteredNumber;
end
end
disp(['The biggest number entered is: ' num2str(biggestNumber)]);
You don't need to limit it to positive numbers but to answer your question, you can do this. And remove the || in < 0 to allow the user to choose negative numbers.
num = [];
while (true)
in = input('Enter a number or a non-numeric character to quit: ');
if isempty(in) || ~isnumeric(in) || in < 0
break
end
num(end+1) = in;
end
[M, INDEX] = max(num);
if ~isempty(num)
disp(['The ', num2str(INDEX),' was the maximum entered value and was: ', num2str(M), '.'])
else
disp('No value entered.')
end
I have 5 items:
A,B,C,D,E
I need to assign them to 2x8 array, but I need to place similar items far as possible and if possible simetric place
For example if the amount of each items contain following
A= 2, B= 2, C=3, D=4, E=5
Manually I can do the following
E C D A B C E D
D E C B A D E E
I tried too many thing with random, but the program hangs in loop
Could you give me any idea
Thanks
private void button2_Click_1(object sender, EventArgs e)
{
Random rnd = new Random();
richTextBox3.Text = "";
// I have 5 kind of material, each of them have the following amount:
// type A=2
// type B=2
// type C=3
// type D=4
// type E=5
int[] materials = { 2, 2, 3, 4, 5 };
int Xa = materials[0]; // material A amount
int Xb = materials[1]; // material B amount
int Xc = materials[2];
int Xd = materials[3];
int Xe = materials[4];
// I need to place them in 2 row * 8 column
// when doing this assignment I should place the similar materials far away as possible
// It possible symetric order
string[,] array = new string[2,8];
Array.Clear(array, 0, array.Length);
array[0, 0] = "E"; // As beginnig I assigned to (0,0) the most biggest material(E)
Xe = Xe - 1;
array[1, 0] = "D";// For the second row (1,0) the second biggest material (D)
Xd = Xd - 1;
// first row
for (int j = 1; j < 8; j++) // I used the first column for D and E material
{
int random = rnd.Next(0, 5); // I randomly choose one of the material
// if the column is blank (not assigned before and
// If the random material is 0 (A type) and the amount of A is not 0
// and the previous column is not assigned with A type
if (array[0,j]=="" && random == 0 && Xa != 0 && array[0,j-1]!="A")
{
array[0,j] = "A"; // assign A to this column
Xa = Xa - 1; // reduce A amount
richTextBox3.Text += "A"; // write "A" in text box
richTextBox3.Text += " ";
}
// B type condition
else if (array[0,j]=="" && random == 1 && Xb != 0 && array[0,j-1]!="B")
{
array[0,j] = "B"; // assign B to this column
Xb = Xb - 1; // reduce B amount
richTextBox3.Text += "B"; // write "B" in text box
richTextBox3.Text += " ";
}
// C type condition
else if (array[0, j] == "" && random == 2 && Xc != 0 && array[0, j - 1] != "C")
{
array[0, j] = "C"; // assign C to this column
Xc = Xc - 1; // reduce C amount
richTextBox3.Text += "C"; // write "C" in text box
richTextBox3.Text += " ";
}
// D type condition
else if (array[0, j] == "" && random == 3 && Xd != 0 && array[0, j - 1] != "D")
{
array[0, j] = "D"; // assign D to this column
Xd = Xd - 1; // reduce D amount
richTextBox3.Text += "D"; // write "D" in text box
richTextBox3.Text += " ";
}
// E type condition
else if (array[0, j] == "" && random == 4 && Xd != 0 && array[0, j - 1] != "E")
{
array[0, j] = "E"; // assign E to this column
Xe = Xe - 1; // reduce E amount
richTextBox3.Text += "E"; // write "C" in text box
richTextBox3.Text += " ";
}
else j = j - 1; // if all above not true try again
}
}