Putting Output values into a matrix.

조회 수: 5 (최근 30일)
Edward Steen
Edward Steen 2017년 2월 24일
댓글: Star Strider 2020년 11월 27일
Hi, I have an if statement within a double for loop. I would like to be able to put the two index values that satisfy the if statement into a matrix that I can then manipulate etc. Currently, I am just using "disp" to display the two index values. How can I go about putting the values into a matrix?

채택된 답변

Star Strider
Star Strider 2017년 2월 24일
편집: Star Strider 2017년 2월 24일
One approach:
vals = [];
for k1 = 1:10
for k2 = 1:10
if k1 == k2 % Test Condition
vals = [vals; k1 k2]; % Store Index Values
end
end
end
Here, the ‘vals’ matrix will be:
vals =
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
  댓글 수: 2
Rashfa Baalfaqih
Rashfa Baalfaqih 2020년 11월 27일
What if I want to put a title for each column?
Star Strider
Star Strider 2020년 11월 27일
Use array2table and then add the variable (column header) names.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by