Reading/Finding data withing column

I have loaded my data into matlab using the uiiport(mydata.txt). It consists of 4 columns, like so:
(0 0 0 0)
(1 0 0 25)
(1 1 0 74)
(1 1 1 122)
(2 0 0 200)
....
I would like to program a way that I can ask for a number as mydata(column1,column2,column3), and have it give me the corresponding number in column 4. For example:
constants = uiimport(mydata.txt)
SomeCommand(1,1,0);
=
74
Is there a simple way to do this? It would also be nice to be able to label the columns with their meaning in the variable editor, but it's not really necessary.

回答 (3 件)

Jonathan Sullivan
Jonathan Sullivan 2013 年 5 月 22 日

0 投票

[~,ind] = ismember([1 1 0],constants(1:3,:),'rows');
constants(ind,4)

3 件のコメント

K
K 2013 年 5 月 22 日
Sorry, I am not familiar with this. Would you mind breaking it down for me a bit?
Roger Stafford
Roger Stafford 2013 年 5 月 22 日
Don't you mean "constants(:,1:3)" in the above, Jonathan?
Jonathan Sullivan
Jonathan Sullivan 2013 年 5 月 23 日
Absolutely. Good catch.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 5 月 23 日

0 投票

constants=[0 0 0 0
1 0 0 25
1 1 0 74
1 1 1 122
0 0 0 200]
a=constants(:,1:3)
[idx,idx]=ismember(a,[1 1 0],'rows')
out=constants(logical(idx),4)
Iain
Iain 2013 年 5 月 23 日

0 投票

constants(find( (constants(:,1) == 1st) & (constants(:,2) == 2nd) & (constants(:,3) == 3rd)),4)
This:
1. Compares each of the first 3 columns against what you supply as 1st, 2nd and 3rd (constants(:,1) == x), takes the "and" of those logical results "&", and returns a single column containing true where all three match, and false elsewhere.
2. find, then gets the row number of EACH true.
3. It then retuns the correct elements of column #4. - If there are none, it returns an empty.

5 件のコメント

K
K 2013 年 5 月 23 日
This looks like exactly what I would like to use. It does not seem to want to recognize the 'find' prompt however. Here is what I have:
constants = uiimport(file.txt,1);
E = constants(find((constants(:,1)==4)&(constants(:,2)==2)&(constants(:,3)==3)),4)
The error message I keep getting is:
"Undefined function 'eq' for input arguments of type 'struct'."
Any advice?
Iain
Iain 2013 年 5 月 23 日
That error indicates that constants is a structure, and not a straightforward matrix.
if you just type constants at command line, what gets printed out to screen?
K
K 2013 年 5 月 23 日
constants =
Constant0x2810x29: [188x4 double]
K
K 2013 年 5 月 23 日
I can see the matrix in the variable editor. Is it better to have the program read it straight from the text file on my computer or to import it using the uiimport()?
Iain
Iain 2013 年 5 月 24 日
It might make a difference, but its probably not worth worrying about unless you have to read in the file(s) lots.
constants has come out as a structure, with a single field with the name "Constant0x2810x29".
Where I have put constants, you should put constants.Constant0x2810x29.
Constant0x2810x29 will probably change from file to file, so you may need something like this:
fn = fieldnames(constant); % Gets a list of the fields (in code)
mydata = getfield(constant,fn{1}); % Gets the 1st field from constant and puts it in mydata.

この質問は閉じられています。

質問済み:

K
K
2013 年 5 月 22 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by