user414981
user414981

Reputation: 397

How can I find specific elements in a matrix in MATLAB?

I have a data set file that has 3 columns in it.

0 0 1
1 0 0
0 1 0

I have the loaded the data file into MATLAB and now I want to check for which column the output "1" is present.

The name of the file is: out.data

In the first row "1" is present in the 3rd column. How do I write it in matlab?

Upvotes: 3

Views: 331

Answers (3)

Matt Mizumi
Matt Mizumi

Reputation: 1193

you can also do

[junk,column_index] = max(data,[],2);

then column_index corresponds the first column in each row that has the 1 (assuming the data is well behaved).

Upvotes: 1

merv
merv

Reputation: 1449

output = [0 0 1 ; 1 0 0 ; 0 1 0];

[~,index] = max(output, [], 2)
index =
     3
     1
     2

Upvotes: 3

ysap
ysap

Reputation: 8125

This is without actually checking it (don't have matlab available right now), but might work:

>> b = a';
>> rem(find(b(:) == 1),3) + 1

Upvotes: 0

Related Questions