Add values to a Matrix

조회 수: 5 (최근 30일)
Antonio Jayena
Antonio Jayena 2015년 3월 25일
답변: Andrew Newell 2015년 3월 26일
Hello!
I have the following code:
p=0;
num=0:1:3;
cont=0;
i=0;
b=0;
n=0;
for k=1:num(end)
cad = [];
cad = 'scope_%d_1.csv';
ss = [];
ss = sprintf(cad,num(k))
s = csvread(ss, 2, 0);
[fil,col]=size(s);
for i=1:fil
if ((s(i,2))< 0.5)&&(s(i+1,2)-(s(i,2))>2)&& p>0
cont=cont+1;
n=s(i, 1);
display(s(i, 1)-b);
p=p-1;
else if ((s(i,2))< 0.5)&&(s(i+1,2)-(s(i,2))>2)&& p==0
cont=cont+1;
display(s(i, 1)-n);
b=s(i, 1);
p=p+1;
else if (cont==25)
p=0;
num=0:1:3;
cont=0;
i=0;
b=0;
n=0;
break
end
end
end
end
end
And my intention is to, (in each iteration of any of both IF loops), save in a M matrix all the values that appears in the display (for all the iterations).
It is possible?
Thank you
  댓글 수: 2
Andrew Newell
Andrew Newell 2015년 3월 25일
Hello, Antonio. When you have loops, it's much easier to understand the logic if you indent them, so I have done that for you. See Improve Code Readability.
Having done that, I still find it hard to understand your code or determine what values you want saving in a matrix. I suspect that, where you are writing else if, you really mean elseif. I also suggest that you look at your code in the MATLAB editor and fix all the problems that have an orange underline.
Antonio Jayena
Antonio Jayena 2015년 3월 25일
Thank you for your help Im new on Matlab. Yes you are right with the elseif problem.
I want to save all the values of all the iterations in a Matrix 1xn (of the IF's loops) of
(s(i, 1)-b)
and
(s(i, 1)-n)
In each iteration

댓글을 달려면 로그인하십시오.

답변 (1개)

Andrew Newell
Andrew Newell 2015년 3월 26일
If you're trying to collect all the values for the inner loop ( for i=1:fil ), you could try putting
M = [];
above the loop, and replacing
display(s(i, 1)-b);
by
M = [M s(i, 1) - b];
and
display(s(i, 1)-n);
by
M = [M s(i, 1)-n];
It's better if a vector of the correct size is preallocated before the loop starts, but I don't know how large M will be.

카테고리

Help CenterFile Exchange에서 Beamforming and Direction of Arrival Estimation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by