将 Matlab 多维元胞数组转换为 Javascript 数组的最佳方法是什么?
What is the best way to convert Matlab multidimensional cell array to Javascript array?
我有一个非常大的 4D Matlab 矩阵 (31x31x86x127),我希望将其转换为 Javascript 4D 数组。做这个的最好方式是什么?
目前我的暂定方法是:
1) 将Matlab矩阵写入二进制文件,然后读入并构建Javascript.
2) 使用 JSONlab (http://www.mathworks.com/matlabcentral/fileexchange/33381-jsonlab--a-toolbox-to-encode-decode-json-files-in-matlab-octave) 将 Matlab 矩阵转换为 JSON 字符串,然后编写自定义解码器将 JSON 字符串转换为一个 Javascript 数组。问题是 JSON 文本文件是 1.98GB...
3) 这可能是最好的方法。
fileID = fopen('test.bin', 'w');
fwrite(fileID,value,'double');
Test.bin 大约是 82MB,这实际上是我所期望的。 31*31*86*127*8 位/双 = 82 MB!但是,我如何(在浏览器中)将这个二进制文件读取到 4d Javascript 数组?谢谢!
想法?
感谢您的帮助!
我认为最好的方法是
- 将矩阵写成字符串或文本文件(不需要二进制文件)。您将需要
n-1
个分隔符,其中 n=4
是您案例的维数。请参阅此 Saturn Fiddle 作为二维矩阵的示例。下面的代码
- 将文本文件读入 JavaScript 字符串。如何执行此操作实际上取决于您是在服务器上还是在 Web 浏览器上使用 JavaScript。
- 将字符串解析为JavaScript数组。您将必须在 (1) 的分隔符上使用
split
函数,然后将它们输入到这样的数组中 example.
代码部分 (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
代码部分 (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);
save
不是写入文本文件的正确函数。使用 savejson
或 saveubjson
并将文件名传递给函数。不要使用这些函数的 return 参数。这样做我得到一个 ubjson 小于 100MB 和一个 json 小于 150MB.
我的原始答案,基于对所用代码的了解不足:
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. you should end up with a reasonable sized data without losing the advantages of a standardized file exchange format.
我有一个非常大的 4D Matlab 矩阵 (31x31x86x127),我希望将其转换为 Javascript 4D 数组。做这个的最好方式是什么?
目前我的暂定方法是:
1) 将Matlab矩阵写入二进制文件,然后读入并构建Javascript.
2) 使用 JSONlab (http://www.mathworks.com/matlabcentral/fileexchange/33381-jsonlab--a-toolbox-to-encode-decode-json-files-in-matlab-octave) 将 Matlab 矩阵转换为 JSON 字符串,然后编写自定义解码器将 JSON 字符串转换为一个 Javascript 数组。问题是 JSON 文本文件是 1.98GB...
3) 这可能是最好的方法。
fileID = fopen('test.bin', 'w');
fwrite(fileID,value,'double');
Test.bin 大约是 82MB,这实际上是我所期望的。 31*31*86*127*8 位/双 = 82 MB!但是,我如何(在浏览器中)将这个二进制文件读取到 4d Javascript 数组?谢谢! 想法?
感谢您的帮助!
我认为最好的方法是
- 将矩阵写成字符串或文本文件(不需要二进制文件)。您将需要
n-1
个分隔符,其中n=4
是您案例的维数。请参阅此 Saturn Fiddle 作为二维矩阵的示例。下面的代码 - 将文本文件读入 JavaScript 字符串。如何执行此操作实际上取决于您是在服务器上还是在 Web 浏览器上使用 JavaScript。
- 将字符串解析为JavaScript数组。您将必须在 (1) 的分隔符上使用
split
函数,然后将它们输入到这样的数组中 example.
代码部分 (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
代码部分 (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);
save
不是写入文本文件的正确函数。使用 savejson
或 saveubjson
并将文件名传递给函数。不要使用这些函数的 return 参数。这样做我得到一个 ubjson 小于 100MB 和一个 json 小于 150MB.
我的原始答案,基于对所用代码的了解不足:
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. you should end up with a reasonable sized data without losing the advantages of a standardized file exchange format.