How I can search for a variable that sastify two conditions

1 次查看(过去 30 天)
Hi,
I have a "file1" with the variables P1, T1, C1 (the variables size can change from 1x1 to 1x30) and "file2" with P2, T2, C2 (they are standard values, always the variables size is 1x20). I need to calculate x=C1-C2 e.g, I need that matlab search which value of C2 it should use when P1==P2 & T1==T2. They are always values of T1=T2 and P1=P2.
What I am doing is loading both files to bring all the variables to my work space and then:
for ll=1:lenght(file1);
ind=find(T1==T2(ll) & P1==P2(ll));
for i=1:lenght(file1)
C3(ind(i))= C2(ll);
end
end
Error: Index exceeds matrix dimensions. Error in Excel_table (line 63) km_eq(ind(i))= km(ll);
And : Always the first values of C3 is zero, it doesn't matter if the size of the variables is 1x2, or 1x10.
I don't have so much experience Matlabing, any suggestion about how to approach the problem is welcome.
Thanks in advance.
  1 个评论
JohnGalt
JohnGalt 2017-4-4
it looks like the second for-loop condition should be over the length of 'ind'
for i = 1:length(ind)
However, I suspect that the C3 variable is not going to be what you want... Think of a specific example... assume that the first time that T1==T2 and P1==P2 in file1 happens at ind = 10. This means that ind(1) = 10. In the inner for-loop, when i=1 ... you are then setting C(ind(i)) = C2(ll)... with i=1 and ind(1) = 10, this means that line becomes C(10) = C2(ll).
I'm still not sure that this is what will solve your problem... perhaps you should include some example of the data you are analysing...
if i was to guess.. what you want to do is.. .
cInd = 1;
for ll = 1:length(file1)
ind=find(T1==T2(ll) & P1==P2(ll));
if ~isempty(ind)
C3(cInd) = C2(ll);
cInd = cInd+1;
end
end

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by