Find value in matrix that matches calculation

First my code does a calculation and sets it equal to ash.
I then need matlab to find all of the values that match ash in column 4 and average them. (The first three numbers should be the same but after the decimal will differ),
But there is a number next to it in column 3 that I need it to store (for each value found in column 4) and average of these numbers as well.
How would I do that.
Here is what I have so far. I look at column 4, starting with the second value to the end I have it compare to the ash value. But it doesn't seem to work.
eledata = (data(2:end,4))==ash;

 Accepted Answer

dpb
dpb on 23 Jun 2017
Edited: dpb on 28 Jun 2017
== is an exact match to the LSB if these values are differing in some decimal position then
eledata = (data(2:end,4))==ash;
will not find any other than those that are identical to what the value stored in variable ash is at the time the code is run.
Try
doc ismembertol
doc uniquetol
for a couple of routines that have facility to consider a tolerance for the comparison.
ADDENDUM
"Hos do I get the column three values with this data as well?"
lia=ismembertol(data(:,2),ash); % use default tolerance
mnAsh=mean(data(lia,2:3)); % mean of those matching in col 2 and 3
The default tol value for double uses 1e-12. If your differences are larger than that, you'll need to set an appropriate tol value and perhaps use the optional 'DataScale' named parameter to set an appropriate tolerance for your specific data.
ADDENDUM 2
With the definition that want the whole part of the elevation column match the rounded distance integer, there's no need for anything exotic--
ix=fix(round(dist)==fix(data(:,2));
mnAsh=mean(data(lx,2:3));
The data themselves are
data(fix(round(dist)==fix(data(:,2),:);
where you don't even need to save the indexing logical array unless have need to make more than a single selection with it.

15 Comments

hi  hey
hi hey on 23 Jun 2017
Edited: hi hey on 23 Jun 2017
Do calculation to get ash
Then read in coulmns 3 and 4 from row 2:end
Then use doc ismembertol to compare values in column 4:end to match ash(within limit)
How do I get the column three values with this data as well?
I'm thinking that I should read the two columns in adds x y data and have the doc ismembertol only look at the second number. The have something grab the first and second number and send it to an array.
EDIT:
I have the data store in
eledata = (data(2:end,3));
But when I run what you put all i only get zeros back.
dpb
dpb on 24 Jun 2017
Edited: dpb on 24 Jun 2017
Would need to see the code and some sample data; probably the tolerance isn't appropriate for the data so nothing was selected.
We'd only need a set of 10 or so values that illustrate the actual number, a couple representative "nearby" values and what would not be; doesn't need to be anything big to check the logic.
NB: I don't have a recent-enough release that incorporates uniquetol so can't physically test it here, but we should be able to divine the issue by reading the doc and looking at the values.
I sent you the code and here is some data. First browse for the file,
The select the first t0/from
then press start
The file contained only one cell w/ the word 'Latitude' and the code is gibberish.
All we need is the specific uniquetol call with the arguments used and a few data points pasted here that give typical numbers you're trying to group/separate. Nothing exotic...
I sent a basic excel file. It just contains four columns with numbers. I'm not sure why it didn't work. But here is another file to try.
hi  hey
hi hey on 26 Jun 2017
Edited: hi hey on 26 Jun 2017
Here is a better version
Same result...see previous comment that you're making this much harder than needs be. There's only one ismembertol line in the .m file and it's commented out.
ONE MORE TIME, show us some data that illustrate the values and the differences you're trying to segregate/congergate and the code you've used to try it -- and that code only...all the rest is just noise.
I have fixed my ways and here is everything simplified lol.
dpb
dpb on 27 Jun 2017
Edited: dpb on 27 Jun 2017
'kay, now we're getting somewhere. Now the question is what, precisely, you mean by "find the values in column 4 to match this number rounded". There are values from ~847.6 up to 849.01 with difference of from .05 - 0.1 between. So, what's the difference that is significant and which way to round?
Is it the integer value computed above (848) +/-0.5, or all >=848 and <849 or what?
How is the band determined; will there be every integer present from min(round(elev)) through max(round(elev)) or can there become large-enough gaps that some values are/may be missing? It makes a difference on how to define boundaries, differences, but you've got to have a specification for what is that grouping.
Oh...second question. Is this for only this one reference value or for a set of values or are you just trying to bin around integer values as above from min:max or what?
Round (dist) to the nearest whole number. dist as of right now equals 847.7143 and I want it to always be the nearest whole number.
It won't always be those numbers. It will vary depending on the drop down menu the user selects. Those numbers (one's in dist(the division part)) represents the first drop down menu option.
The values that are in the excel file, I don't want to be rounded. I just want to check the first three values to be 848 and then once it checks out to be clear. Take the entire number and store it and then look to the left and grab that number as well.
So for the excel sheet it will take the rows 9-22 since they all have 848 as the first three numbers
This will be for multiple (dist) values that are determined by the drop down list.
"I just want to check the first three values to be 848"
Then you don't need anything at all but
ix=fix(round(dist)==fix(data(:,2));
mnAsh=mean(data(ix,2:3));
Now, wasn't that easy once got a clear definition of what wanted? :) vbg
hi  hey
hi hey on 28 Jun 2017
Edited: hi hey on 28 Jun 2017
So I put that in and it says Error: File: Fixed_Elevation_short.m Line: 16 Column: 18
Unexpected MATLAB expression.
Here is line 16, it does not like the x
mnAsh=mean(data(1x,2:3));
Obvious (I'd think?) typo -- '1x' --> 'ix' the index vector just computed.
"Read the code, don't just cut 'n paste! :)"
So what you have written:
ix=fix(round(dist)==fix(data(:,2));
fix rounds the number to nearest zero
round(dist)= takes it to the whole number
== compares
fix((data:,2)) gets the data from the file and then reads all the values from the second column and then rounds to nearest zero
mnAsh=mean(data('ix',2:3));
takes the average from 'ix' values but I'm not sure about 2:3
ix=fix(round(dist)==fix(data(:,2));
There's a superfluous fix there; not sure where that came from but I see I somehow got it in the original.
ix=(round(dist)==fix(data(:,2));
was intended which does the rounding of the dist value to nearest and converts the 2nd column values to the integer portion without rounding--what you said you wanted with those elements whose digits matched the rounded value.
That specification is what makes it unnecessary to have something like ismembertol which would be to get those nearest by absolute difference to the value segregated.
The
mnAsh=mean(data('ix',2:3));
takes the average from 'ix' values ...
Well, it won't work written that way, ix is a logical addressing vector variable, not a text string.
mnAsh=mean(data(ix,2:3));
ix is the row index vector, '2:3' is the column index.

Sign in to comment.

More Answers (0)

Asked:

on 23 Jun 2017

Commented:

dpb
on 28 Jun 2017

Community Treasure Hunt

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

Start Hunting!