Loop on two matrices

4 次查看(过去 30 天)
Tarek
Tarek 2021-2-2
编辑: Jan 2021-2-2
I have these two matrices:
TL=[ 21;36;31;8;34;22;14;34;4;51]
TTB=[207;92;133;1;99;1;127;197;228;42]
I want to create a vector that takes eache value of TTB ans repeat it for Tl(i) time; also each time it should ad a +1.
for exemple: 207 should be added in a matrice lets name it A, 21 times as A(1)=207, A(2)=208, A(3)=209......A(21)=207+21
i hope it is clear ;
thank you
  1 个评论
Jan
Jan 2021-2-2
If A(1) is 207, A(21) should be 207 + 20, not +21.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2021-2-2
编辑:Jan 2021-2-2
TL = [ 21;36;31;8;34;22;14;34;4;51];
TTB = [207;92;133;1;99;1;127;197;228;42];
C = cell(1, numel(TL));
for k = 1:numel(TL)
C{k} = TTB(k) + (0:TL(k)-1);
end
Result = [C{:}];
[EDITED] A one-liner version:
Result = cell2mat(cellfun(@(a, b) a + (0:b-1), ...
num2cell(TTB), num2cell(TL), 'uniformoutput', 0).');
I'd prefer the loop, because it is easier to read.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by