Convert List to Double Array by LINQ (C#3.0) - c#-3.0

I have two lists x1 and x2 of type double
List<double> x1 = new List<double> { 0.0330, -0.6463};
List<double> x2 = new List<double> { -0.2718, -0.2240};
I am using the below function to convert it to double array
List<List<double>> xData = new List<List<double>> { x1, x2 };
double[,] xArray = new double[xData.Count, xData[0].Count];
for (int i = 0; i < xData.Count; i++)
{
for (int j = 0; j < xData[0].Count; j++)
{
xArray[i, j] = xData[i][j];
}
}
Is it possible to do the same stuff (i.e. the function that is converting List to array) by using Linq.
Using : (C#3.0) & Framework - 3.5
I want to do this by using Linq because I am learning it but not have sufficient knowledge to write that.
Thanks
Convert List to Double Array by LINQ (C#3.0)

There may be some way of doing it, but I don't believe LINQ has any built-in support for rectangular arrays.

Related

Need to pick indexes which does not contains any value in a List<List<String> matrix tic tac toe

I have a nested list, like List<List> matrix ( this matrix contains values like [ {"X","","O"}, {"","X",""}, {"","","O"}])
Need to Pick only those indexes from this nested list which is empty, like empty matrix =[(0,1), (1,0),(1,2),(2,0),(2,1)] in a list. After that I want to return only one value from the empty matrix , like new value = (1,0);
my code is here
List<String> checkEmptyFields() {
final playervalue = matrix;
final n = countMatrix;
var element;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (!(playervalue[i][j].contains("X") ||
playervalue[i][j].contains("O"))) {
storeEmptyFields= jsonDecode(playervalue[i][j]);
}
}
element = storeEmptyFields.values.toList(); // need to pick only one value and
return like (0,2)
return element;
}
return element;
}

Why does my buffer geometry fail when I try to load in vertices, faces and normal from .mat file?

I want to load in my matlab geometry into my three.js scene. My 3D data is saved in a struct .mat file which contains .vertices, .faces, .VertexNormals and .VertexColorData arrays. I am able to load it into JavaScript and use buffer geometry and set attributes to store the data into a mesh geometry.
var keyName = keysArray[0];
meshGeometry = new THREE.BufferGeometry();
var index = 0;
var positions = new Float32Array(bfjson.data[keyName].vertices.length * 3);
for (let i = 0; i < bfjson.data[keyName].vertices.length; i++) {
positions[index++] = bfjson.data[keyName].vertices[i][0];
positions[index++] = bfjson.data[keyName].vertices[i][1];
positions[index++] = bfjson.data[keyName].vertices[i][2];
}
meshGeometry.setAttribute(
'position',
new THREE.BufferAttribute(positions, 3));
var index = 0;
var vectornormals = new Float32Array(bfjson.data[keyName].VertexNormals.length * 3);
for (let i = 0; i < bfjson.data[keyName].VertexNormals.length; i++) {
vectornormals[index++] = bfjson.data[keyName].VertexNormals[i][0];
vectornormals[index++] = bfjson.data[keyName].VertexNormals[i][1];
vectornormals[index++] = bfjson.data[keyName].VertexNormals[i][2];
}
meshGeometry.setAttribute(
'normal',
new THREE.BufferAttribute(vectornormals, 3));
var index = 0;
//var faces = new Uint16Array(bfjson.data[keyName].faces.length * 3);
var faces = [];
for (let i = 0; i < bfjson.data[keyName].faces.length; i++) {
faces[index++] = bfjson.data[keyName].faces[i][0];
faces[index++] = bfjson.data[keyName].faces[i][1];
faces[index++] = bfjson.data[keyName].faces[i][2];
}
meshGeometry.setIndex(faces);
// default color attribute
const colors = [];
for (let i = 0, n = meshGeometry.attributes.position.count; i < n; ++i) {
colors.push(1, 1, 1);
}
meshGeometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3));
for (let i = 0; i < bfjson.data[keyName].CData.length; i++) {
CData[i] = (bfjson.data[keyName].CData[i]);
}
meshGeometry.setAttribute('perfusion', new THREE.Float32BufferAttribute(CData, 1));
mesh.geometry = meshGeometry;
updateColors();
The vertex coloring works fine. However, I end up with mesh with index faces or normal not connecting into a normal surface.
mesh with index faces or normal not connecting up into a normal surface.
I am not sure what I am doing wrong. I will be extremely grateful for any help provided.
Edit--
I have made a jsfiddle to help.
https://jsfiddle.net/marieO/5zdhsk78/68/
But you need to download the .mat file first then upload it to the scene. (as I was unable to add it to the jsfiddle)
https://dev.kingsvirtualanatomyandhistology.kcl.ac.uk//models/mat/p21_newmod.mat
Thanks for posting a working example with the steps needed to reproduce the error. It makes it much easier to help.
You have 652 vertices in your Matlab geometry. When using .setIndex(), these indices have to be in the [0, 651] range, because JavaScript arrays start at index 0. However, your faces data ranges from [1, 652], which means all your triangles are off by 1 vertex.
This is easily solvable by adding a -1 when assigning the index:
var index = 0;
var faces = [];
for (let i = 1; i < bfjson.data[keyName].faces.length; i++) {
faces[index++] = bfjson.data[keyName].faces[i][0] - 1;
faces[index++] = bfjson.data[keyName].faces[i][1] - 1;
faces[index++] = bfjson.data[keyName].faces[i][2] - 1;
}
meshGeometry.setIndex(faces);
Result:

How to get n power ( square of a number or cube etc.) of a number in flutter?

I have two values one for base that is X and one for power N, how can a get X to the power of N ans.
any code will be appreciated.
You are looking for this:
https://api.dartlang.org/stable/2.5.0/dart-math/pow.html
so:
pow(X,N)
If you want to implement it, you can have a look at here:
https://coflutter.com/challenges/dart-how-to-implement-exponential-function-power/
This boils down to this loop:
int power(int x, int n) {
int retval = 1;
for (int i = 0; i < n; i++) {
retval *= x;
}
return retval;
}
This only works well for integer n-s.
For all of these examples with pow you need the following import:
import 'dart:math';
8²
final answer = pow(8, 2); // 64
Notes:
If you are only squaring, then it's probably easier to do this:
final answer = 8 * 8;
answer is inferred to be of type num, which could be an int or double at runtime. In this case the runtime type is int, but in the following two examples it is double.
Fourth root of 256
final answer = pow(256, 1/4); // 4.0
0.2^(-3)
final answer = pow(0.2, -3); // 124.99999999999999
That's basically the same as five cubed.
btw Its working fine for double also
double power() {
double x= double.parse(t1.text);
int y= int.parse(t2.text);
double power = 1;
for (double i = 0; i < y; i++) {
power *= x;
} ;
return power;
}

Porting signal windowing code from Matlab to Java

This is part of a code from spectral subtraction algorithm,i'm trying to optimize it for android.please help me.
this is the matlab code:
function Seg=segment(signal,W,SP,Window)
% SEGMENT chops a signal to overlapping windowed segments
% A= SEGMENT(X,W,SP,WIN) returns a matrix which its columns are segmented
% and windowed frames of the input one dimentional signal, X. W is the
% number of samples per window, default value W=256. SP is the shift
% percentage, default value SP=0.4. WIN is the window that is multiplied by
% each segment and its length should be W. the default window is hamming
% window.
% 06-Sep-04
% Esfandiar Zavarehei
if nargin<3
SP=.4;
end
if nargin<2
W=256;
end
if nargin<4
Window=hamming(W);
end
Window=Window(:); %make it a column vector
L=length(signal);
SP=fix(W.*SP);
N=fix((L-W)/SP +1); %number of segments
Index=(repmat(1:W,N,1)+repmat((0:(N-1))'*SP,1,W))';
hw=repmat(Window,1,N);
Seg=signal(Index).*hw;
and this is our java code for this function:
public class MatrixAndSegments
{
public int numberOfSegments;
public double[][] res;
public MatrixAndSegments(int numberOfSegments,double[][] res)
{
this.numberOfSegments = numberOfSegments;
this.res = res;
}
}
public MatrixAndSegments segment (double[] signal_in,int samplesPerWindow, double shiftPercentage, double[] window)
{
//default shiftPercentage = 0.4
//default samplesPerWindow = 256 //W
//default window = hanning
int L = signal_in.length;
shiftPercentage = fix(samplesPerWindow * shiftPercentage); //SP
int numberOfSegments = fix ( (L - samplesPerWindow)/ shiftPercentage + 1); //N
double[][] reprowMatrix = reprowtrans(samplesPerWindow,numberOfSegments);
double[][] repcolMatrix = repcoltrans(numberOfSegments, shiftPercentage,samplesPerWindow );
//Index=(repmat(1:W,N,1)+repmat((0:(N-1))'*SP,1,W))';
double[][] index = new double[samplesPerWindow+1][numberOfSegments+1];
for (int x = 1; x < samplesPerWindow+1; x++ )
{
for (int y = 1 ; y < numberOfSegments + 1; y++) //numberOfSegments was 3
{
index[x][y] = reprowMatrix[x][y] + repcolMatrix[x][y];
}
}
//hamming window
double[] hammingWindow = this.HammingWindow(samplesPerWindow);
double[][] HW = repvector(hammingWindow, numberOfSegments);
double[][] seg = new double[samplesPerWindow][numberOfSegments];
for (int y = 1 ; y < numberOfSegments + 1; y++)
{
for (int x = 1; x < samplesPerWindow+1; x++)
{
seg[x-1][y-1] = signal_in[ (int)index[x][y]-1 ] * HW[x-1][y-1];
}
}
MatrixAndSegments Matrixseg = new MatrixAndSegments(numberOfSegments,seg);
return Matrixseg;
}
public int fix(double val) {
if (val < 0) {
return (int) Math.ceil(val);
}
return (int) Math.floor(val);
}
public double[][] repvector(double[] vec, int replications)
{
double[][] result = new double[vec.length][replications];
for (int x = 0; x < vec.length; x++) {
for (int y = 0; y < replications; y++) {
result[x][y] = vec[x];
}
}
return result;
}
public double[][] reprowtrans(int end, int replications)
{
double[][] result = new double[end +1][replications+1];
for (int x = 1; x <= end; x++) {
for (int y = 1; y <= replications; y++) {
result[x][y] = x ;
}
}
return result;
}
public double[][] repcoltrans(int end, double multiplier, int replications)
{
double[][] result = new double[replications+1][end+1];
for (int x = 1; x <= replications; x++) {
for (int y = 1; y <= end ; y++) {
result[x][y] = (y-1)*multiplier;
}
}
return result;
}
public double[] HammingWindow(int size)
{
double[] window = new double[size];
for (int i = 0; i < size; i++)
{
window[i] = 0.54-0.46 * (Math.cos(2.0 * Math.PI * i / (size-1)));
}
return window;
}
"Porting" Matlab code statement by statement to Java is a bad approach.
Data is rarely manipulated in Matlab using loops and addressing individual elements (because the Matlab interpreter/VM is rather slow), but rather through calls to block processing functions (which have been carefully written and optimized). This leads to a very idiosyncratic programming style in which repmat, reshape, find, fancy indexing et al. are used to do operations which would be much more naturally expressed through Java loops.
For example, to multiply each column of a matrix A by a vector v, you will write in matlab:
A = diag(v) * A
or
A = repmat(v', 1, size(A, 2)) .* A
This solution:
for i = 1:size(A, 2),
A(:, i) = A(:, i) .* v';
end;
is inefficient.
But it would be terribly foolish to try to do the same thing in Java and invoke a matrix product or to build a matrix with repeated copies of v. Instead, just do:
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
a[i][j] *= v[i]
}
}
I suggest you to try to understand what this matlab function is actually doing, instead of focusing on how it is doing it, and reimplement it from scratch in Java, forgetting all the matlab implementation except the specifications given in the comments. Half of the code you have written is useless, indeed. Actually, it seems to me that this function wouldn't be needed at all, and what it does could be efficiently integrated in the caller's code.

How to make double[,] x_List in C#3.0?

I ned to implement the multi-linear regression in C#(3.0) by using the LinESt function of Excel.
Basically I am trying to achieve
=LINEST(ACL_returns!I2:I10,ACL_returns!J2:K10,FALSE,TRUE)
So I have the data as below
double[] x1 = new double[] { 0.0330, -0.6463, 0.1226, -0.3304, 0.4764, -0.4159, 0.4209, -0.4070, -0.2090 };
double[] x2 = new double[] { -0.2718, -0.2240, -0.1275, -0.0810, 0.0349, -0.5067, 0.0094, -0.4404, -0.1212 };
double[] y = new double[] { 0.4807, -3.7070, -4.5582, -11.2126, -0.7733, 3.7269, 2.7672, 8.3333, 4.7023 };
I have to write a function whose signature will be
Compute(double[,] x_List, double[] y_List)
{
LinEst(x_List,y_List, true, true); < - This is the excel function that I will call.
}
My question is how by using double[] x1 and double[] x2 I will make double[,] x_List ?
I am using C#3.0 and framework 3.5.
Thanks in advance
double[,] xValues = new double[x1.Length, x2.Length];
for (int i = 0; i < x1.Length; i++)
{
xValues[i, 0] = x1[i];
xValues[i, 1] = x2[i];
}
Actually it should be
double[,] xValues = new double[x1.Length, x2.Length];
int max = (new int[]{ x1.Length,x2.Length}).Max();
for (int i = 0; i < max; i++)
{
xValues[0, i] = x1.Length > i ? x1[i] : 0;
xValues[1, i] = x2.Length > i ? x2[i] : 0;
}
Samir is right, that i should call Max() once outside the iterator rather than each iteration, i've amended this.
The side of the multi-dimensional array is incorrect in both your answer and bablo's. In addition, the call to Max on every iteration in bablo's answer seems really slow, especially with large numbers of elements.
int max = (new int[] { x1.Length, x2.Length }).Max();
double[,] xValues = new double[2, max];
for (int i = 0; i < max; i++)
{
xValues[0, i] = x1.Length > i ? x1[i] : 0;
xValues[1, i] = x2.Length > i ? x2[i] : 0;
}