简体   繁体   English

在 Matlab 中加速 for 循环

[英]Speed up for loop in Matlab

I have the following for loop which makes my program runs very slow when the file size is very big.我有以下for循环,这使我的程序在文件大小非常大时运行速度非常慢。 What is the best way to vectorize it.向量化它的最佳方法是什么。

I read data from a PLY file as here using the command, data = textread(fileName, '%s','delimiter', '\\n');我读data作为来自PLY文件这里使用命令, data = textread(fileName, '%s','delimiter', '\\n'); . . data is an*1 cell array. data是一个 *1 元胞数组。 Each element in data is a 7*1 cell array of char values. data中的每个元素都是一个 7*1 的 char 值元胞数组。 Sample data file is here .示例数据文件在这里

for i = 1:length(data)-1
    pc(i, 1) = (str2double(cellstr(data{i,1}{1,1})));
    pc(i, 2) = (str2double(cellstr(data{i,1}{2,1})));
    pc(i, 3) = (str2double(cellstr(data{i,1}{3,1})));
    pc(i, 4) = (str2double(cellstr(data{i,1}{4,1})));
    pc(i, 5) = (str2double(cellstr(data{i,1}{5,1})));
    pc(i, 6) = (str2double(cellstr(data{i,1}{6,1})));
end

This will do what you want这会做你想做的

>> pc = str2double([data{:}].')
pc =
    0.1033   -0.2737    0.8570  221.0000  196.0000  174.0000  255.0000
    0.1054   -0.2731    0.8550  220.0000  195.0000  173.0000  255.0000
    ...
    0.1139   -0.2803    0.8490  221.0000  194.0000  172.0000  255.0000
    0.1117   -0.2829    0.8500  225.0000  200.0000  178.0000  255.0000

You can remove the last column and row (as appears to be done in your question) with您可以删除最后一列和行(如您的问题中所示)

>> pc = pc(1:end-1, 1:end-1)
pc =
    0.1033   -0.2737    0.8570  221.0000  196.0000  174.0000
    0.1054   -0.2731    0.8550  220.0000  195.0000  173.0000
    ...
    0.1139   -0.2803    0.8490  221.0000  194.0000  172.0000

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

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