- Do NOT name variables like you have your y -- makes it impossible to do anything generically with them after have done (as you're learning and probably the crux of the problem).
- Ignoring that problem, to build an array with repeated elements, KRON() is oftentimes handy--
How to use 'for' in this example?
1 view (last 30 days)
Show older comments
CHOI HYUNDUK
on 5 Mar 2019
Answered: Andrei Bobrov
on 5 Mar 2019
I have some code for matrix.
I want to use 'for' for this matrix.
I tried that, but I don't know how to make that.
Could you help me"?
aaa = [y11(1), y22(1),y33(1),y44(1),y55(1),y66(1),...
y11(1), y22(1),y33(1),y44(1),y55(1),y66(1),...
y11(2), y22(2),y33(2),y44(2),y55(2),y66(2),...
y11(2), y22(2),y33(2),y44(2),y55(2),y66(2),...
y11(3), y22(3),y33(3),y44(3),y55(3),y66(3),...
y11(3), y22(3),y33(3),y44(3),y55(3),y66(3),...
y11(4), y22(4),y33(4),y44(4),y55(4),y66(4),...
y11(4), y22(4),y33(4),y44(4),y55(4),y66(4),...
y11(5), y22(5),y33(5),y44(5),y55(5),y66(5),...
y11(5), y22(5),y33(5),y44(5),y55(5),y66(5),...
y11(6), y22(6),y33(6),y44(6),y55(6),y66(6),...
y11(6), y22(6),y33(6),y44(6),y55(6),y66(6),...
y11(7), y22(7),y33(7),y44(7),y55(7),y66(7),...
y11(7), y22(7),y33(7),y44(7),y55(7),y66(7),...
y11(8), y22(8),y33(8),y44(8),y55(8),y66(8),...
y11(8), y22(8),y33(8),y44(8),y55(8),y66(8),...
y11(9), y22(9),y33(9),y44(9),y55(9),y66(9),...
y11(9), y22(9),y33(9),y44(9),y55(9),y66(9),...
y11(1), y22(1),y33(1),y44(1),y55(1),y66(1),...
y11(1), y22(1),y33(1),y44(1),y55(1),y66(1),...
y11(2), y22(2),y33(2),y44(2),y55(2),y66(2),...
y11(2), y22(2),y33(2),y44(2),y55(2),y66(2),...
y11(3), y22(3),y33(3),y44(3),y55(3),y66(3),...
y11(3), y22(3),y33(3),y44(3),y55(3),y66(3),...
y11(4), y22(4),y33(4),y44(4),y55(4),y66(4),...
y11(4), y22(4),y33(4),y44(4),y55(4),y66(4),...
y11(5), y22(5),y33(5),y44(5),y55(5),y66(5),...
y11(5), y22(5),y33(5),y44(5),y55(5),y66(5),...
y11(6), y22(6),y33(6),y44(6),y55(6),y66(6),...
y11(6), y22(6),y33(6),y44(6),y55(6),y66(6),...
y11(7), y22(7),y33(7),y44(7),y55(7),y66(7),...
y11(7), y22(7),y33(7),y44(7),y55(7),y66(7),...
y11(8), y22(8),y33(8),y44(8),y55(8),y66(8),...
y11(8), y22(8),y33(8),y44(8),y55(8),y66(8),...
y11(9), y22(9),y33(9),y44(9),y55(9),y66(9),...
y11(9), y22(9),y33(9),y44(9),y55(9),y66(9)]
2 Comments
dpb
on 5 Mar 2019
You give absolutely no klews as what you wanted the for to do, but...
>> kron([1 2 3;4 5 6],ones(2,1))
ans =
1 2 3
1 2 3
4 5 6
4 5 6
>>
will duplicate each row in A, B times. I'm going to presume you want the matrix as you formatted it not as wrote it which will end up in one long vector.
If you instead write yii(j) as Y(i,j), then
A=kron(Y,ones(2,1));
should build your desired array.
Adam
on 5 Mar 2019
aaa = repelem( [y11(:) y22(:) y33(:) y44(:) y55(:) y66(:)], 2, 1 );
should work too, although as dpb says, you shouldn't have 6 arrays named like that in the first place ideally.
Accepted Answer
Andrei Bobrov
on 5 Mar 2019
a = [y11(:)';y22(:)';y33(:)';y44(:)';y55(:)';y66(:)'];
aaa = reshape(repelem(a,1,2),1,[]);
0 Comments
More Answers (0)
See Also
Categories
Find more on Language Fundamentals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!