matlab拓展矩阵,在matlab中扩展矩阵? (expand matrix in matlab?)

Let us assume some working code to resemble your pseudo-code.

%// Original code

for i = 1:10

if rand(1)>0.5

data1 = rand(2,1)

K = [data1(1) data1(2) i]

end

end

Changes for "pushing data without initialization/pre-allocation":

To save data at each iteration we keeping on "stacking" data along a chosen dimension. This could be thought of as pushing data. For a 2D case, use either a "row vector push" or a "column vector push". For this case we are assuming a former case.

We don't index into K using the original iterator, but use a custom one,

that only increments when the condition is satisfied.

The code below must make it clear.

%// Modified code

count = 1; %// Custom iterator; initialize it for the iteration when condition would be satisfied for the first time

for i = 1:10

if rand(1)>0.5

data1 = rand(2,1)

K(count,:) = [data1(1) data1(2) i] %// Row indexing to save data at each iteration

count = count +1; %// We need to manually increment our custom iterator

end

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值