I want to Add different values to all the elements of a column vector succesively.., plz help me out

1 回表示 (過去 30 日間)
for example: I have a column vector as [1200 1260 1320 1380 1440 1500 1560 1620......] I want to add a value 0 to 1200, 15 to 1260, 30 to 1320,45 to 1380 then again 0 to 1440, 15 to 1500, 30 to 1560, 45 to 1620...... and so on, such that only 4 values have to be added to a big column of infinite elements, but repeatedly.., plz help me come out

採用された回答

Stephen23
Stephen23 2017 年 2 月 14 日
This is easy using mod, no loops are required (and would be slow and inefficient anyway):
>> X = [1200;1260;1320;1380;1440;1500;1560;1620]
X =
1200
1260
1320
1380
1440
1500
1560
1620
>> V = 15*mod(0:numel(X)-1,4);
>> X+V(:)
ans =
1200
1275
1350
1425
1440
1515
1590
1665

その他の回答 (1 件)

KSSV
KSSV 2017 年 2 月 14 日
a = [1200 1260 1320 1380 1440 1500 1560 1620] ;
a0 = [0 15 30 45 0 15 30 45] ;
N = length(a) ;
iwant = zeros(N,4) ;
for i = 1:N
iwant(i,:) = linspace(a0(i),a(i),4) ;
end
iwant = reshape(iwant',[],1)
  3 件のコメント
Mohammed Yousuf
Mohammed Yousuf 2017 年 2 月 14 日
my matrix a is very big like: some 100000 elements in it, i want to add 0 to first element 15 to second, 30 to third, 45 to fourth and again 0 to the fifth 15 to the sixth 30 to the seventh and 45 to the eighth and so on....
KSSV
KSSV 2017 年 2 月 14 日
You want to add or insert middle values?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by