how to get combination of matrix columns

조회 수: 2 (최근 30일)
Monika  Kok
Monika Kok 2016년 5월 3일
답변: Guillaume 2016년 5월 3일
I would like to get all the possible combination between two column of a matrix.
supoose my matrix is
matrix =
[1 1 1 1
1 0 0 0
1 0 1 0
0 1 1 0]
combination between column 1 and column 4 can be
column 1 and column 4
column 1 and column 2 and column 4
column 1 and column 3 and column 4
column 1 and column 2 and column 3 and column 4
so column 1 and column 4 should be present in every case.
[I want this using for loop]

답변 (1개)

Guillaume
Guillaume 2016년 5월 3일
Why are 1 and 4 singled out specifically?
As this sounds like homework, I'm not giving you the code, but you can simply:
  1. use a for loop to iterate between 0 and the number of column - number of column singled out, call the index k.
  2. use nchoosek to select all combinations of k elements out of the index of the remaining columns
  3. use the indices returned by nchoosek plus your singled out columns to extract the column combinations.

카테고리

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