Problem with reaching specific values of an array or matrix

Hello. I have a for loop, for each value of counter, I want to find the relevent raw value when rs==1 or ep==1 from various excel files.
If rs==1, I want it to write t_c's relevant value corresponding its raw value.
If ep==1, I want it to write t_c's relevant value corresponding its raw value + 1.
However, when I consider raw value + 1, I get zero values (even without if loop). I do not understand why this happens so? When I try to find its relevant value for raw value, I get true values (without the if loop). I would be happy if I solved this question.Thank you. :)
Below is a part of my code:
ep=mydata(:,1);
counter=mydata(:,2);
rs=mydata(:,3);
t_c=mydata(:,4);
t=[];
raw=[];
for n=0:4
f=1;
raw(f)=find((counter == n) & ((rs==1) | (ep==1)))
y(f)=raw(f)+1
if (rs==1)
te(f)=t_c(raw(f))
else (ep==1)
te(f)=t_c(y(f))
end
t=[t te(f)]
C=table(t);
f=f+1;
end

7 件のコメント

Jan
Jan 2022 年 8 月 15 日
There are no "if loops" in any programming language I know.
What does this mean: "when I consider raw value + 1, I get zero values (even without if loop)"
This part is useless:
for n = 0:4
f = 1; % f is set to 1
...
f = f+1; % Now f is 2 - but reset in the next iteration
end
Go Sugar
Go Sugar 2022 年 8 月 15 日
編集済み: Jan 2022 年 8 月 15 日
I am sorry I misused the word loop. I mean if statements.
Lets think of it like this. I have an excel table. What I am trying to do is finding y values from the found intersections for each n value :
raw(f)=find((counter == n) & ((rs==1) | (ep==1)))
After finding y (raw) values, I am trying to find relevant x values for t_c. We may think of it like a function.
For relevant coloums having rs==1, I can easily find x values for t_c by using the obtained y (raw) values.
But for relevant coloums having ep==1, I need to find the corresponding x value by using y+1 value. I am not able to obtain this value, I get zero values. I tried to do this using if statements.
I hope my problem is more understandable now. Thank you. :)
dpb
dpb 2022 年 8 月 15 日
Give us a sample dataset and show us what you're trying to produce; I get lost in the verbal description -- it's not at all clear what you really mean by "for relevant coloums having ep==1, I need to find the corresponding x value by using y+1 value. "
Jan
Jan 2022 年 8 月 15 日
This line looks strange:
else (explode==1)
I guess you mean elseif.
Go Sugar
Go Sugar 2022 年 8 月 15 日
編集済み: Go Sugar 2022 年 8 月 15 日
Thank you so much for all the comments and suggestions. I have solved the problem in another way. I used another variable from the excel file in if else statement. It did not give any error and produced relevant x values from raw-y values correctly. :)
Have a good day.
dpb
dpb 2022 年 8 月 15 日
I'd suggest posting your final code as an Answer, then -- you can Accept your own. Serves to let folks know the problem is sovled if nothing else.
Go Sugar
Go Sugar 2022 年 8 月 15 日
Ok, I have just posted it. Thank you so much for the quick replies. :)

サインインしてコメントする。

 採用された回答

Go Sugar
Go Sugar 2022 年 8 月 15 日
編集済み: Go Sugar 2022 年 8 月 15 日

0 投票

This is the finalized version of the code. It is working as expected. Thank you for all the help. :)
ep=mydata(:,1);
counter=mydata(:,2);
rs=mydata(:,3);
t_c=mydata(:,4);
a=mydata(:,5);
t=[];
raw=[];
for n=0:4
f=1;
raw(f)=find((counter == n) & ((rs==1) | (ep==1)));
y(f)=raw(f)+1;
if (a==0)
te(f)=t_c(raw(f));
else
te(f)=0;
end
t=[t te(f)]
C=table(t);
f=f+1;
end

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2022 年 8 月 15 日

編集済み:

2022 年 8 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by