i need to develop android studio that learn from machine - android

float[] confidences = outputFeature0.getFloatArray();
int maxPos = 0;
float maxConfidence = 0;
for (int i = 0; i < confidences; i++){
if (confidences[(int) i] > maxConfidence){
maxConfidence = confidences[i];
maxPos = i;
}
}

Related

How to print a full pyramid pattern?

My expected output
I tried the following code, but it prints only one *. I am confused where the code has went wrong.
int rows = 6, k = 0;
for(int i = 1; i <= rows; ++i, k = 0) {
for(int space = 1; space <= rows - i; ++space) {
t2.setText(" ");
}
while(k != 2 * i - 1) {
t2.setText("* ");
++k;
}
t2.setText();
}
This will work like a charm...
int y = 5,x = 5;
for (int i = 0; i < y; i++)
{
for (int j = 0; j < x-i; j++)
{
System.out.print(" ");
}
for (int k = 0; k < i+1; k++)
{
System.out.print("* ");
}
System.out.print("\n");
}
Try This:
```
Scanner sc = new Scanner(System.in);
char s;
int l;
System.out.println("Enter the Letter : ");
s = sc.next().charAt(0);
l = Character.getNumericValue(s)-9;
for (int i = 0; i < l; i++)
{
for (int j = 0; j < (l - i); j++)
System.out.print(" ");
for (int j = 0; j <= i; j++)
System.out.printf("%c%s",(char)j+65,"");
for (int j = i; j > 0; j--)
System.out.printf("%c%s",(char)j+64,"");
System.out.println();
}
```
The OP could call this:
String s = createCharPyramid(5, "*");
t2.setText(s);
and define this method:
private String createCharPyramid(int numberOfRows, String mySymbol){
String s = "";
String sep = System.lineSeparator();
for(int i = 0; i < numberOfRows; i++){
for(int j = 0; j < (numberOfRows - i); j++){
s = s + " ";
}
for(int j = 0; j <= i; j++){
s = s + mySymbol;
}
for(int j = i; j > 0; j--){
s = s + mySymbol;
}
s = s + sep;
}
return s;
}
This answer is based on MrRobot's answer. His answer is a pure java solution and would correctly show a pyramid in the console. So, if you like this answer give him a +1.

Print reverse pyramid of numbers

I am able to print pyramid like this :
1
123
12345
1234567
Code i used to print the pyramid of numbers like above is :
int a = 1;
int b = 4;
for (int i = 1 ; i <= 4 ; i++){
for (int c = 1 ; c <= b - 1 ; c++){
text.append(" ");
}
for (int k = 1 ; k <= a ; k++){
String result = String.valueOf(k);
text.append(result);
}
a = a + 2;
b--;
text.append("\n");
}
but, the issue i am facing is i have to print the same pyramid, but in reverse order like this :
1234567
12345
123
1
Any help would be appreciated?
try this ...
for (int i = 7; i > 0; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.print("\n");
if (i % 2 != 0) {
i = i - 1;
}
}
Try this
int a = 1;
int b = 4;
for (int i = 1; i <= 4; i++) {
for (int k = a; k >= 1; k--) {
String result = String.valueOf(k);
text.append(result);
}
for (int c = 1; c <= b - 1; c++) {
text.append(" ");
}
a = a + 2;
b--;
text.append("\n");
}
System.out.println(text.reverse());
Try this :)
int a = 1;
int b = 4;
int m = 4;
for (int i = 1; i <= 4; i++) {
for (int c = 1; c <= m; c++) {
text.append(" ");
}
for (int k = 1; k <= b; k++) {
String result = String.valueOf(k);
text.append(result);
}
a = a + 2;
b--;
m++;
text.append("\n");
}
int numberofdigits = 7;
int numberofdigitsforrow = 0;
int Emptyspace = 0;
for (int i = 0; i < numberofdigits; i++)
{
numberofdigitsforrow = numberofdigits - i * 2;
if (numberofdigitsforrow > 0)
{
Emptyspace = numberofdigits-numberofdigitsforrow;
if (Emptyspace > 0)
{
for (int b = 1; b <= Emptyspace/2; b++)
{
Console.Write(" ");
}
}
for (int c = 1; c <= numberofdigitsforrow; c++)
{
Console.Write(c.ToString());
}
if (Emptyspace > 0)
{
for (int b = 1; b <= Emptyspace / 2; b++)
{
Console.Write(" ");
}
}
Console.WriteLine();
}
}
int w = 7;
System.out.println("Here is your pattern....!!!");
for (int i = 1; i <= 5; i++)
{
//Printing i spaces at the beginning of each row
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
//Printing i to rows value at the end of each row
for (int j = 1; j <=w; j++)
{
System.out.print(j+" ");
}
w = w - 2;
System.out.println();
}

Android: How to generate unique random numbers in an array and perform bubble sort?

The following is my code but it doesn't generate unique random numbers
Random rand = new Random();
int[] array = new int[n];
for (int i = 0; i < n; i++){
array[i] = rand.nextInt(n)+1;
}
for (int i = 0; i < ( n - 1 ); i++)
{
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
public TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView);
Random r = new Random();
StringBuffer temp =new StringBuffer("Random numbers:");
for(int i=0;i<10;i++) {
int i1 = r.nextInt(100 - 0) + 0;
temp.append(String.valueOf(i1));
temp.append(String.valueOf(" "));
}
tv.setText(temp.toString());
}
//here ,,I generate 10 random numbers and save it in to stringBuffer and dispaly it in to textView..here range is up to 0 to 100...you can take your own Range ...I hope ,,It will help you...
check this out!... it works for me
Random rand = new Random();
int n = 10;
int[] array = new int[n];
for (int i = 0; i < n; i++){
array[i] = (int) (rand.nextDouble() * n + 1);
}
for (int i = 0; i < ( n - 1 ); i++)
{
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for (int piece: array) {
System.out.println(piece);
}

Android Zipper Animation for unlock screen

I am currently working on zip animation to unlock android mobile screen. Changing background images is a expensive task and have not a smooth effect. I want a smooth effect in it. Any help please? Thanks
Try this:
The smooth effect makes use of Convolution Matrix:
Some image effects are better to implement using Convolution Matrix
method like: Gaussian Blur, Sharpening, Embossing, Smooth…
Check That Link to know more about Convolution Matrix or Another one
To do Convolution Matrix
import android.graphics.Bitmap;
import android.graphics.Color;
public class ConvolutionMatrix
{
public static final int SIZE = 3;
public double[][] Matrix;
public double Factor = 1;
public double Offset = 1;
public ConvolutionMatrix(int size) {
Matrix = new double[size][size];
}
public void setAll(double value) {
for (int x = 0; x < SIZE; ++x) {
for (int y = 0; y < SIZE; ++y) {
Matrix[x][y] = value;
}
}
}
public void applyConfig(double[][] config) {
for(int x = 0; x < SIZE; ++x) {
for(int y = 0; y < SIZE; ++y) {
Matrix[x][y] = config[x][y];
}
}
}
public static Bitmap computeConvolution3x3(Bitmap src, ConvolutionMatrix matrix) {
int width = src.getWidth();
int height = src.getHeight();
Bitmap result = Bitmap.createBitmap(width, height, src.getConfig());
int A, R, G, B;
int sumR, sumG, sumB;
int[][] pixels = new int[SIZE][SIZE];
for(int y = 0; y < height - 2; ++y) {
for(int x = 0; x < width - 2; ++x) {
// get pixel matrix
for(int i = 0; i < SIZE; ++i) {
for(int j = 0; j < SIZE; ++j) {
pixels[i][j] = src.getPixel(x + i, y + j);
}
}
// get alpha of center pixel
A = Color.alpha(pixels[1][1]);
// init color sum
sumR = sumG = sumB = 0;
// get sum of RGB on matrix
for(int i = 0; i < SIZE; ++i) {
for(int j = 0; j < SIZE; ++j) {
sumR += (Color.red(pixels[i][j]) * matrix.Matrix[i][j]);
sumG += (Color.green(pixels[i][j]) * matrix.Matrix[i][j]);
sumB += (Color.blue(pixels[i][j]) * matrix.Matrix[i][j]);
}
}
// get final Red
R = (int)(sumR / matrix.Factor + matrix.Offset);
if(R < 0) { R = 0; }
else if(R > 255) { R = 255; }
// get final Green
G = (int)(sumG / matrix.Factor + matrix.Offset);
if(G < 0) { G = 0; }
else if(G > 255) { G = 255; }
// get final Blue
B = (int)(sumB / matrix.Factor + matrix.Offset);
if(B < 0) { B = 0; }
else if(B > 255) { B = 255; }
// apply new pixel
result.setPixel(x + 1, y + 1, Color.argb(A, R, G, B));
}
}
// final image
return result;
}
}
Then to do Smooth effect
public static Bitmap smooth(Bitmap src, double value) {
ConvolutionMatrix convMatrix = new ConvolutionMatrix(3);
convMatrix.setAll(1);
convMatrix.Matrix[1][1] = value;
convMatrix.Factor = value + 8;
convMatrix.Offset = 1;
return ConvolutionMatrix.computeConvolution3x3(src, convMatrix);
}
You can change values and get the smooth effect as you want.
That tutorial it's found HERE

Fast Fourier transform of the recorded wav file in Android

There is a wav file on SD card. First, I connect to a file and reads the byte:
File file = null;file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/202.wav");
byte[] byteData = new byte[(int) file.length()];
FileInputStream in = null;
try {
in = new FileInputStream( file );
in.read( byteData );
in.close();}
I then use the transformation of data from a byte type in type double, because the FFT procedure does not work with byte type:
double[] transformed = new double[(int) file.length()];
for (int j=1;j<8194;j++) {
transformed[j] = (double)byteData[j]; }
And then the procedure of FFT processing this data:
public void spektr(double[] x, double[] y, int Dim, int D, double[] xx, double[] yy) {
int I,J,N,L,K,LE,LE1,IP,NV2,NM1;
double Arg,U1,U2,U3,C,S,T1,T2,T3,T4;
N = (int) Math.pow(2,Dim);
Log.v("N", "N "+N);
for (L = 1; L < Dim; L++){
LE = (int) Math.pow(2,Dim+1-L);
LE1 = LE/2;
U1 = (double)1.0;
U2 = (double)0.0;
Arg = (double)Math.PI/LE1;
C = (double)Math.cos(Arg);
S = D*(double)Math.sin(Arg);
for(J = 1; J < LE1; J++){
I = J;
do{
IP = I+LE1;
T1 = x[I] + x[IP];
T2 = y[I] + y[IP];
T3 = x[I] - x[IP];
T4 = y[I] - y[IP];
x[IP] = T3*U1 - T4*U2;
y[IP] = T4*U1 + T3*U2;
x[I] = T1;
y[I] = T2;
I = I + LE;
}while(I <= N);
U3 = U1*C - U2*S;
U2 = U2*C - U1*S;
U1 = U3;
}
}
NV2 = N / 2;
Log.v("NV2", "NV2 "+NV2);
NM1 = N-1;
Log.v("NM1", "NM1 "+NM1);
J = 1;
for(I = 1; I < NM1; I++){
if (I < J){
T1 = x[J];
T2 = y[J];
x[J] = x[I];
y[J] = y[I];
x[I] = T1;
y[I] = T2;
}
K = NV2;
while (K < J){
J = J - K;
K = K / 2;
};
J = J + K;
}
for(I = 1; I < N; I++){
x[I] = x[I] / N*2;
y[I] = y[I] / N*2;
}
for(I = 1; I < N; I++){
xx[I] = x[I];
yy[I] = y[I];
}
xx[1] = (double)0.0;
}
Reads the file successfully, but the processing procedure of the FFT is not working.
How to find the FFT procedure, or give the code to check the FFT result?
here is some free library to do FFT in java.

Categories

Resources