简体   繁体   English

将 Matlab 多维元胞数组转换为 Javascript 数组的最佳方法是什么?

[英]What is the best way to convert Matlab multidimensional cell array to Javascript array?

I have a very large 4D Matlab matrix (31x31x86x127) that I wish to convert into a Javascript 4D array.我有一个非常大的 4D Matlab 矩阵(31x31x86x127),我希望将其转换为 Javascript 4D 数组。 What is the best way to do this?做这个的最好方式是什么?

Currently my tentative approach will be to either:目前我的暂定方法是:

1) Write the Matlab matrix into a binary file, and then read that in and build the Javascript. 1)将Matlab矩阵写入二进制文件,然后读入并构建Javascript。

2) Use JSONlab ( http://www.mathworks.com/matlabcentral/fileexchange/33381-jsonlab--a-toolbox-to-encode-decode-json-files-in-matlab-octave ) to convert the Matlab matrix into a JSON string and then write a custom decoder to turn that JSON string into a Javascript Array. 2) 使用 JSONlab ( http://www.mathworks.com/matlabcentral/fileexchange/33381-jsonlab--a-toolbox-to-encode-decode-json-files-in-matlab-octave ) 将 Matlab 矩阵转换为JSON 字符串,然后编写自定义解码器以将该 JSON 字符串转换为 Javascript 数组。 Issue is that the JSON text file is 1.98GB...问题是 JSON 文本文件是 1.98GB ...

3) This may be the best way. 3)这可能是最好的方法。

fileID = fopen('test.bin', 'w'); fwrite(fileID,value,'double');

Test.bin is then around 82MB, which is actually what I expect. Test.bin 大约为 82MB,这实际上是我所期望的。 31*31*86*127*8bits/double = 82ish MB! 31*31*86*127*8bits/double = 82 左右 MB! However, how do I then read (in the browser) this binary file to a 4d Javascript array?但是,我如何(在浏览器中)将此二进制文件读取到 4d Javascript 数组? Thanks!谢谢! Thoughts?想法?

Thanks for your help!谢谢你的帮助!

save is not the right function to write a text file. save不是编写文本文件的正确函数。 Use savejson or saveubjson and pass the filename to the function.使用savejsonsaveubjson并将文件名传递给函数。 Do not use the return argument of these functions.不要使用这些函数的返回参数。 Doing so I get a ubjson with less than 100MB and a json with less than 150MB.这样做我得到一个小于 100MB 的 ubjson 和一个小于 150MB 的 json。

My original answer, based on insufficient knowledge about the used code:我的原始答案基于对所用代码的了解不足:

Instead of writing your own binary format, use one of the already available binary formats.不要编写自己的二进制格式,而是使用已经可用的二进制格式之一。 Try writing it to universal binary json, jsonlab does support it.尝试将其写入通用二进制 json,jsonlab 确实支持它。 you should end up with a reasonable sized data without losing the advantages of a standardized file exchange format.您应该以合理大小的数据结束,而不会失去标准化文件交换格式的优势。

I think the best way is to我认为最好的方法是

  1. Write the matrix out as a string or text file (binary file is not necessary).将矩阵作为字符串或文本文件写出(不需要二进制文件)。 You will need n-1 delimiters, where n=4 is the number of dimensions for your case.您将需要n-1分隔符,其中n=4是您的案例的维数。 See this Saturn Fiddle as an example for a 2D matrix.请参阅此Saturn Fiddle作为 2D 矩阵的示例。 Code below下面的代码
  2. Read the text file into a JavaScript string.将文本文件读入 JavaScript 字符串。 How you do this really depends on if you're using JavaScript on the server or web browser.您如何做到这一点实际上取决于您是在服务器还是 Web 浏览器上使用 JavaScript。
  3. Parse the string into a JavaScript array.将字符串解析为 JavaScript 数组。 You will have to use the split function on the delimiters from (1) and then enter them into an array like this example .您必须对 (1) 中的分隔符使用split函数,然后将它们输入到一个数组中,如本例所示

Code part (1):代码部分(1):

% Welcome to SaturnAPI!
% Start collaborating with MATLAB-Octave fiddles and accomplish more.
% Start your script below these comments.
A = [ 1 2 3 ; 4 5 6 ; 7 8 9 ]
for ii=1:size(A)(1)
    for jj=1:size(A)(2)
        printf(" %d ", A(ii,jj));
  end
  printf(";");
end

Code part (3):代码部分(3):

function make(dim, lvl, arr) {
  if (lvl === 1) return [];
  if (!lvl) lvl = dim;
  if (!arr) arr = [];
  for (var i = 0, l = dim; i < l; i += 1) {
    arr[i] = make(dim, lvl - 1, arr[i]);
  }
  return arr;
}

var myMultiArray = make(4);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM