How to specify a stop criterion in an if loop.
Show older comments
I have two files named TData (j) (technical Data) and RData (i) in an array format. I also have a file NFile (i), which is supposed to specify an operation condition as follows. If an index of NFile is greater than 1, we define a newfile, NewRData (i) as the value of TData (j) that is IMMEDIATELY less than or equal to RData(i).
I have written the following code and the code seems to stop at the last value of the TData instead of the immediate less value of TData.
RData =[0.443 0.641 0.641 0.641 0.641 0.641] % 6 by 1 column matrix
NFile= [0.93 0.95 0.97 0.99 1.02 1.01] % 1 by 6 row matrix
TData =[1.2 0.87 0.64 0.44 0.32 0.25 0.21] % 7 by 1 column matrix
for i=1:size(RData,1)
for j=1:size(TData,1)
if NFile(i)>1
if TData(j)<RData(i)
NewRData(i)= TRData(j)
else
NewR(i)=R(i)
end
end
end
based on the above file, my NewR should be [0.443 0.641 0.641 0.641 0.44 0.44]
However my program give NewR as [0 0 0 0 0.21 0.21]. Kindly help me trouble shoot it. What am I missing?
6 Comments
Walter Roberson
on 3 Jul 2021
NewRData(i)= TRData(j)
You never use that variable name again, and there is a possibility that you will never assign to the variable at all.
Rafael Hernandez-Walls
on 3 Jul 2021
I do not know is correct this sintaxis:
RData =[0.443 0.641 0.641 0.641 0.641 0.641]; % 6 by 1 column matrix
NFile= [0.93 0.95 0.97 0.99 1.02 1.01]; % 1 by 6 row matrix
TData =[1.2 0.87 0.64 0.44 0.32 0.25 0.21]; % 7 by 1 column matrix
NRdata=length(RData);
NTdata=length(TData);
for i=1:NRdata
for j=1:NTdata
if NFile(i)>1
if TData(j)<RData(i)
NewR(i)= TData(j)
end
else
NewR(i)=RData(i)
end
end
end
Lewis Waswa
on 3 Jul 2021
Edited: Lewis Waswa
on 3 Jul 2021
Walter Roberson
on 3 Jul 2021
I suspect what you are looking for is the break command.
dpb
on 3 Jul 2021
I can't follow how, specifically, you get the result you claim is the right answer in the original Q?
IF is not a looping construct is, it seems to me, the first major conceptual problem here; the use of a double loop over i,j causes the stepping through the two vectors to go through the second, inner loop each time for each element of the outer -- that, since there is only one element being stored for each index, causes the previous N-1 loops of the outer loop to be overwritten by the results of the last.
I think need to back up to the problem definition and clarify just exactly what it is that is the actual condition being looked for -- if it is the thing about NFILE>1 is one result and <=1 another, looks to me like logical addressing could solve the problem without looping.
But, need the actual conditions to be clearly and unambiguously defined as to the desired result first before can write any algorithm.
Lewis Waswa
on 9 Jul 2021
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!