matlab符号数字计算,matlab:有没有一种方法可以加速计算数字的符号?

当数组的大小很大时,我程序中的瓶颈是为数组中的所有数字计算一个数字的符号。我在下面展示了我尝试过的两种方法,都有相似的结果。我有16GB的内存,阵列占用了~5GB。我看到的问题是符号函数占用了大量的RAM+虚拟内存。有人知道如何减少内存需求并加快将数组输入符号放入数组输出的过程吗(见下文)?

在if或switch命令中使用for循环不会耗尽内存,但需要一个小时才能完成(太长时间)。

size = 1e9; % size of large array (just an example, could be larger)

output = int8(zeros(size,1)-1); % preallocate to -1

input = single(rand(size,1)); % create random array between 0 and 1

scalar = single(0.5); % just a scalar number, set to 0.5 (midpoint) for example

% approach 1 (comment out when using approach 2)

output = int8(sign(input - scalar)); % this line of code uses a ton of RAM and virtual memory

% approach 2

output(input>scalar) = 1; % this line of code uses a ton of RAM and virtual memory

output(input==scalar) = 0; % this line of code uses a ton of RAM and virtual memory

提前谢谢你的建议。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值