Algorithm for a Loop in a Table

조회 수: 3 (최근 30일)
ercan duzgun
ercan duzgun 2021년 8월 14일
댓글: Sulaymon Eshkabilov 2021년 8월 14일
Dear Matlab users,
I am trying to write a program with a loop. Assume that for each of the element of L, there are two solutions of M, and two solutions of N. I want to write them all on a table.
L=[1,2]'
M=[10,20,60,70]'
N=[100,200,500,600]'
Table=[
L(1) M(1) N(1)
L(1) M(1) N(2)
L(1) M(2) N(1)
L(1) M(2) N(2)
L(2) M(3) N(3)
L(2) M(3) N(4)
L(2) M(4) N(3)
L(2) M(4) N(4)]
Table should be like this. However, L vector doesn't have to have two elements. It might have more elements. If L has the third element for instance, the next line would be L(3) M(5) N(5).
Can you recommend how to write a Matlab code to generate this table?
Thanks in advance

채택된 답변

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 8월 14일
In your case, you'd need to use hand entry. However, if your pattern is repeating then, you can employ replem() and get these.
L=[1, 2]'; L=repelem(L,4);
M=[10,20,60,70]'; M=repelem(M,2);
N=[100,200,500,600]'; N=repelem(N,2);
T= array2table([L, M, N], 'VariableNames', {'L', 'M', 'N'})
T = 8×3 table
L M N _ __ ___ 1 10 100 1 10 100 1 20 200 1 20 200 2 60 500 2 60 500 2 70 600 2 70 600
  댓글 수: 2
ercan duzgun
ercan duzgun 2021년 8월 14일
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 8월 14일
Most welcome

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Simon Chan
Simon Chan 2021년 8월 14일
Try this:
Col1 = repelem(L,4);
Col2 = repmat(M',2,1);
Col3 = repmat([N(1:2:end),N(2:2:end)]',2,1);
Table = [Col1 Col2(:) Col3(:)];

카테고리

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