Need help writing script using "if" "then" type arguments and loops to pull specific data from long data file
显示 更早的评论
I have a data file that contains 3 columns (A, B, C). Column A is an indexing file and Columns B and C contain data. I would like to generate a new, two column matrix that contains only specific data from Columns B and C that are dependent on events in Column A. I need matlab to find the value in Column B when Column A switches from 3 to 2 (the value in Column B remains constant during switching) and find the value in Column B when Column A switches from 2 to 3 (the value in Column C remains constant during switching). The desired output file for the data provided here is:
0.00081966 / 0.00057264
0.0014508 / 0.00087722
0.0019136 / 0.0011055
0.0022935 / 0.0012946
(the text shows "/" between columns to be clear)
I believe I can do this with an "if" "then" type scenario (ie if the second row in column A minus the first row in Column A = 1, then the new value should be the value in Column B, etc), but cannot figure out how to loop it through the entire column A. Any help is really appreciated! Thank you!
采纳的回答
更多回答(1 个)
Roger Stafford
2015-7-10
Rather than using 'if' constructs I think it would be better to use logical indexing, or perhaps in this case the 'find' function. Here is an example. Call your three-columned matrix, 'M'.
f = find(diff(M(:,1)) ~= 0); % Find where 1st column changes
R = M(f,2:3); % Save only the corresponding 2nd and 3rd columns in R
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!