Using indexing for two arrays of different lengths

5 次查看(过去 30 天)
I have two separate arrays, x = 1244 x 1 and y = 1244 x 1050. I want to be able to index specific values within the x array and then find the corresponding index's within the y array. So far my code reads:
x_1= find(x == 799.6585468680)
x_2 = find(x == 3995.72148499300)
x_3 = find(x == 2021.00198661820)
y_1 = find(y == x_1)
y_2 = find(y == x_2)
y_3 = find(y == x_3)
The values input here are specifically from my data. This reads out:
x_1 =
1
x_2 =
1244
x_3 =
476
y_1 =
0×1 empty double column vector
y_2 =
0×1 empty double column vector
y_3 =
0×1 empty double column vector
However this gives me a load of empty double column vectors, I know I should be looping it to make sure I get the xth index value from every column in the y array, but I am very lost as to how I achieve this. Is there an obvious function I should be using?

回答(1 个)

Guillaume
Guillaume 2017-9-14
The first issue with what you're doing is with
x == some_non_integer_value
It will work for some values. For others, it won't, e.g:
x = 0.1 + 0.1 + 0.1 % == 0.3 right?
x == 0.3 % guess not, return false, find would return empty!
This is due to the way numbers are stored on computers. Not all decimal values can be stored exactly (e.g 0.1 can't), meaning that testing for exact decimal value is dangerous. The proper way to compare floating point values is to check that their absolute difference is smaller than a value much smaller than their magnitude:
abs(x - 0.3) <= 1e-10 %test for equality
Second issue:
y_1 = find(y == x_1)
Remember that x_1, if not empty, is an index, always integer, from 1 to 1244. That is what you're searching for in y. From your description, I don't think that's what you meant to do. The above line will also fail if the find for x_1 returned more than index (i.e. the search value is present more than once in x).
"I know I should be looping". Most likely not. It is likely that it can all be done in a few lines with no loop. E.g, all your x_i could be found in one go with
searchvalues = [799.6585468680, 3995.72148499300, 2021.00198661820];
[found, where] = ismembertol(searchvalues, x)
where would correspond to your [x_1, x_2, x_3]
A better explanation of what you're trying to do with y (provide an example with numbers) would allow me to complete this answer.
  1 个评论
mabinj
mabinj 2017-9-15
First of all thank you very much for your help! I'm just starting in matlab and all the help i can get I really appreciate. Ok, here is the code in full:
files = 'E:\Second full run\';
filepattern = fullfile(files, '*.csv');
thefiles = dir(filepattern);
for j = 1: length(thefiles)
filename = thefiles(j).name;
fullfilename = fullfile(files, filename);
fprintf(1, 'Now reading %s\n', fullfilename);
if j == 1
dataArray = csvread(fullfilename);
x = dataArray(:,1)
y = dataArray(:,2)
else
y = [y , csvread(fullfilename,0,1)];
end
end
x_1= find(x == 799.6585468680)
x_3 = find(x == 3995.72148499300)
x_2 = find(x == 2021.00198661820)
y_1 = find(y == x_1)
y_2 = find(y == x_2)
y_3 = find(y == x_3)
I read in 1050 CSV files all of which have two columns, wavelength and absorption. The absorption is changing from file to file whilst the wavelength remains uniform throughout all files.
What I want to do is pick specific wavelength values, then index them from the x array and locate the corresponding absorption values in the y array (for each column in the y array).
The reason I then said I would have to loop this, is that I imagined it would have to iterate through each y-column in the y array to do this. And after obtaining all of these y values I then want to plot linear functions for each using y = mx + c, but I think I know how to do that (hopefully).
Thanks again for the help!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by