# Output cell values and column number for matrix with specific values

4 views (last 30 days)
Emma Kuttler on 18 Apr 2022
Edited: Stephen23 on 19 Apr 2022
Hi,
I have a 105 x 13 matrix where the first two columns represent an identifier and columns 3:13 have values either 0 or 1. What I'd like to do is output the first two columns, and then the column number for cells that have a value of 1. I'd like the column numbering to start at column 3, however, so something that is in actual column 13 should have the value 11.
For example, if this is my matrix A I want to produce B:
A = [ 1 128 1 0 0 0 0 0 0 0 0 0 0
1 129 0 1 1 1 0 1 1 0 0 0 1
1 130 1 0 1 0 0 0 0 1 0 0 0
1 149 0 0 0 0 1 0 1 1 0 0 0
1 150 1 1 0 0 1 1 1 0 0 0 0
1 154 1 1 1 1 1 1 0 0 1 1 1]
% I'd like to produce a matrix of the "locations" of the 1 values in
% columns 3:13.
B =
[1 128 1
1 129 2
1 129 3
1 129 4
1 129 6
1 129 7
1 129 11
1 130 1 . . ]
##### 0 CommentsShow -1 older commentsHide -1 older comments

Sign in to comment.

### Answers (2)

Voss on 18 Apr 2022
A = [1 128 1 0 0 0 0 0 0 0 0 0 0
1 129 0 1 1 1 0 1 1 0 0 0 1
1 130 1 0 1 0 0 0 0 1 0 0 0
1 149 0 0 0 0 1 0 1 1 0 0 0
1 150 1 1 0 0 1 1 1 0 0 0 0
1 154 1 1 1 1 1 1 0 0 1 1 1];
B = A(:,[1 2]);
A(:,[1 2]) = [];
[c,r] = find(A.');
B = [B(r,:) c]
B = 27×3
1 128 1 1 129 2 1 129 3 1 129 4 1 129 6 1 129 7 1 129 11 1 130 1 1 130 3 1 130 8
##### 0 CommentsShow -1 older commentsHide -1 older comments

Sign in to comment.

Stephen23 on 19 Apr 2022
Edited: Stephen23 on 19 Apr 2022
Without changing the input matrix A:
A = [1,128,1,0,0,0,0,0,0,0,0,0,0;1,129,0,1,1,1,0,1,1,0,0,0,1;1,130,1,0,1,0,0,0,0,1,0,0,0;1,149,0,0,0,0,1,0,1,1,0,0,0;1,150,1,1,0,0,1,1,1,0,0,0,0;1,154,1,1,1,1,1,1,0,0,1,1,1]
A = 6×13
1 128 1 0 0 0 0 0 0 0 0 0 0 1 129 0 1 1 1 0 1 1 0 0 0 1 1 130 1 0 1 0 0 0 0 1 0 0 0 1 149 0 0 0 0 1 0 1 1 0 0 0 1 150 1 1 0 0 1 1 1 0 0 0 0 1 154 1 1 1 1 1 1 0 0 1 1 1
M = A(:,3:end).';
[C,R] = find(M);
B = [A(R,1:2),C]
B = 27×3
1 128 1 1 129 2 1 129 3 1 129 4 1 129 6 1 129 7 1 129 11 1 130 1 1 130 3 1 130 8
##### 0 CommentsShow -1 older commentsHide -1 older comments

Sign in to comment.

### Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by