在3d矩阵的第二维中进行排序(sortrows in the 2nd dimension of 3d matrix)
我试图在Matlab的3d矩阵的第二维中排序第一行,但sortrows()在这种情况下不起作用。
例如,我有一个像矩阵
a(:,:,1) 1 2 4 3 1 6 2 0 5 a(:,:,2) 3 5 6 1 2 4 0 2 2
我需要得到结果
a(:,:,1) 1 2 4 2 0 5 3 1 6 a(:,:,2) 0 2 2 1 2 4 3 5 6
有没有一种有效的方法来实现这一目标? 非常感谢!
I am trying to sort first row in the 2nd dimension of a 3d matrix in Matlab, but sortrows() doesn't work in this case.
For example, I have a matrix like
a(:,:,1) 1 2 4 3 1 6 2 0 5 a(:,:,2) 3 5 6 1 2 4 0 2 2
and I need to get the result
a(:,:,1) 1 2 4 2 0 5 3 1 6 a(:,:,2) 0 2 2 1 2 4 3 5 6
Is there an efficient way to achieve that? Thanks a lot!
原文:https://stackoverflow.com/questions/30412770
最满意答案
最简单的方法,通过使用正则表达式提取浮点值并通过float / decimal / double或您需要的任何数据类型转换它们。
修改后,您可以再次使用相同的正则表达式替换字符串。
Regex regex = new Regex(@"[1-9][0-9]*\.?[0-9]*([Ee][+-]?[0-9]+)?"); string testString ="1/(2.342/x) * x^3.45"; MatchCollection collection = regex.Matches(testString); foreach(Match item in collection) { double extract =Convert.ToDouble(item.Value); //change your decimal here... testString = testString.Replace(item.Value, extract.ToString()); }
正则表达式信用: https : //stackoverflow.com/a/2293793/2084262
The simplest way to that, to extract floating values by using regex and convert them via float/decimal/double or whatever data type you need.
After your modification, you can replace your string using same regex again.
Regex regex = new Regex(@"[1-9][0-9]*\.?[0-9]*([Ee][+-]?[0-9]+)?"); string testString ="1/(2.342/x) * x^3.45"; MatchCollection collection = regex.Matches(testString); foreach(Match item in collection) { double extract =Convert.ToDouble(item.Value); //change your decimal here... testString = testString.Replace(item.Value, extract.ToString()); }
regex credits to : https://stackoverflow.com/a/2293793/2084262
相关问答
更多-
System.Json不允许您查看整数和浮点数之间的差异。 您可能想尝试Json.NET,它可以: var parsed = JObject.Parse("{\"key1\":12, \"key2\":23.5 }"); foreach (JProperty node in parsed.Children()) { Console.WriteLine("{0}: {1}", node.Name, node.Value.Type); } 输出: key1: Integer key2: Float ...
-
如何在C ++中读取字符串中用空格分隔的多个双精度数?(How to read multiple doubles that are in a string, separated by spaces, in C++?)[2023-01-14]
你可以使用stringstream string str = "3 - 90.2 85.54 93.0"; stringstream ss(str); int num; ss >> num; //read 3 char c; ss >> c; // read "-" double d1; ss >> d1; // read 1 double d2; ss >> d2; // read 2 double d2; ss >> d2; // read 3 you can use st ... -
在C#中,通过规范化修改传入的双精度数组的过程?(In C#, Procedure that modifies the incoming array of doubles by normalizing?)[2021-08-09]
干得好: static void Normalize(double[] numbers) { double max = numbers.Max(); for (int i = 0; i < numbers.Length; i++) { numbers[i] = numbers[i] / max; } } .Net小提琴样本在这里 Here you go: static void Norm ... -
你不能从C#调用该函数。 它接受一个不能用于互操作的std::string 。 您还省略了C#转换中的两个参数。 C ++代码应该是: int Eye_GetPositionSC2( const wchar_t* filename, double sensors[9], int &map_x, int &map_y ) C#代码应该是: [DllImport(@"eyeWhere.dll", CallingConvention = CallingConvention. ...
-
将包含由空格分隔的数字的字符串转换为双精度列表c#(Convert string containing numbers separated by spaces into list of doubles c#)[2021-11-03]
尝试这个: var splitted = yourString.Split(new []{" "}, StringSplitOptions.RemoveEmptyEntries); existingListOfDoubles.AddRange(splitted.Select(double.Parse)); 在ideone试试。 编辑以匹配问题 Try this: var splitted = yourString.Spl ... -
您可以使用DataTable,或者如果您想要更轻量级和强类型的东西,那么创建自己的自定义数据结构。 public struct MyCustomData { String ticker; String cusip; double shares; double price; double value; } 从中做出一个阵列 MyCustomData[] data = new MyCustomData[5000]; . . . data[row].ticker = t ...
-
您可以通过以下几种方式映射此功能: public interface MyLibrary extends Library { void examplefunct(Pointer dd); void examplefunct(java.nio.Buffer dd); void examplefunct(double[] dd); } 如果分配com.sun.jna.Memory的实例,则可以使用各种内存访问器函数将双精度写入缓冲区。 如果您使用直接NIO ByteBuffer,则可以 ...
-
C# - 修改字符串中的双精度数(C# - modify doubles in string)[2024-04-17]
最简单的方法,通过使用正则表达式提取浮点值并通过float / decimal / double或您需要的任何数据类型转换它们。 修改后,您可以再次使用相同的正则表达式替换字符串。 Regex regex = new Regex(@"[1-9][0-9]*\.?[0-9]*([Ee][+-]?[0-9]+)?"); string testString ="1/(2.342/x) * x^3.45"; MatchCollection collection = r ... -
将字符串转换为C中的双精度数(Convert string to doubles in C)[2023-03-29]
这是一个XY问题 。 你真的需要“ fscanf()字符逐行”吗? 这是否导致您在错误的方向上问太多问题? 考虑一下: %lf表示将字符转换为您选择的double ...当没有更合适的字符要转换时它会立即停止...并且换行符不适合转换的字符...是否有灯泡照在你的头上,但是? 在您的情况下,格式字符串中%lf的空格会导致丢弃有用的信息(无论空格是否为换行符)。 停! 你走得太远,结果是你现在需要一个中间字符数组转换函数,这是不必要的膨胀。 通过这个新发现的实现,从格式字符串中删除空格将导致将后固定换行留在流 ... -
对于一般问题,我会使用Schwartzian变换 ,这是Perl中使用的常见模式。 将底层方法移植到.net应该很容易 由于您可以完全控制数据访问,因此您的情况更简单: var tuples = from dc in GetDashboardData group dc by dc.serviceType into g select new{ Cost = g.Sum(sc=>sc.serviceCost), ...