problem with nested loop
이전 댓글 표시
Hi, I am trying to write a code with nested for loop and if statement. I want to create a matrix in such way that, for one value of y, I have different values of x and z, and again for another value of y, I have same values for x but different values of z.
load a.dat;
x=a(:,1);
y=a(:,2); z=a(:,3); for j=min(y):1:max(y)
for i=1:10
if j=10
A(:,i)=z(i)
else if
j=j+1
end
end
end
I am not able to apply if statement for y.
Could anyone please help me?
댓글 수: 4
Guillaume
2015년 4월 30일
I don't understand your question fully, and your piece of code is incomplete, so please give at least some example of input and desired output.
Stephen23
2015년 4월 30일
This could probably be written much neater using some vectorized code, which would likely also be faster.
Instead of providing us with (broken) code to interpret, it would help us if you actually described exactly what you are trying to achieve, giving precise descriptions of the inputs and outputs.
Even better would be if you also include some test values/variables for both the input and output arrays, to give us something to test our own code with.
We know MATLAB quite well so writing the code is not a problem, but what we don't know is what you are trying to do...
For a start: how should the values of A defined? What specifies their values?
Matlabuser
2015년 4월 30일
Stephen23
2015년 4월 30일
If you upload your actual data matrix then we can try this too! Please upload the data (in a textfile or .mat file) using the paperclip button that you will find above the textbox, and note that you will need to push both buttons: Choose file and Attach file.
채택된 답변
추가 답변 (1개)
Søren Jensen
2015년 4월 30일
0 개 추천
A(:,i)= z(i)
do you mean A(:,i)= z(:,i) or A(:,i)= z(i,:)?
When is A defined?
i see 2 for statements and 1 if statement, and only 2 "end", so some of your code seems to be missing.. what is the purpose of the code?
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!