0

When i'm running this code in matlab, it is printing the g4 value into the array columns were g3 and its calculated value is suppose to be, as well as its own column. I was just wondering how to stop g4 being placed into g3's column and instead, print g3 and its value in the two arrays.

Cheers

v_meas = 0;
g1 = 1.09;
g2 = 0.9;
g3 = 0.93;
g4 = 0.85;
radius = 3.75;
K = 0.006;
m = g1;
g = g3;

for ii = 1 : 1 : 2
    v_meas = m*((radius^2)*pi)*K;
    ArrayOfDarceys(1,ii) = v_meas;
    ArrayOfGradients(1,ii) = m;
    v_meas = 0;
    m = g2;
    for jj = 3 : 1 : 4
         v_meas = g*((radius^2)*pi)*K;
         ArrayOfDarceys(1,jj) = v_meas;
         ArrayOfGradients(1,jj) = g;
         v_meas = 0;
         g = g4;
    end
end
ArrayOfDarceys
ArrayOfGradients
3
  • Could you post the output (please, no screenshots) as well and highlight where it has gone wrong, please... Commented Sep 2, 2015 at 9:32
  • The 3rd value in the arrayofgradients should be 0.93, not 0.85. Also the third value in the arrayofdarcys should be the value for 0.93, not 0,85. ArrayOfDarceys = 0.2889 0.2386 0.2253 0.2253 ArrayOfGradients = 1.0900 0.9000 0.8500 0.8500 Commented Sep 2, 2015 at 10:16
  • Could you update the post, please rather than the comments. Commented Sep 2, 2015 at 10:54

1 Answer 1

2

I suspect you didn't intend to nest your for loops. Try this:

for ii = 1 : 1:  2
    v_meas = m*((radius^2)*pi)*K;
    ArrayOfDarceys(1,ii) = v_meas;
    ArrayOfGradients(1,ii) = m;
    v_meas = 0;
    m = g2;
end
for jj = 3 : 1: 4
     v_meas = g*((radius^2)*pi)*K;
     ArrayOfDarceys(1,jj) = v_meas;
     ArrayOfGradients(1,jj) = g;
     v_meas = 0;
     g = g4;
end

If I understand what you are trying to do you could significantly simplify your code though. There is, in fact, no need for any for loops:

ArrayofGradients = [1.09,0.9,0.93,0.85]
ArrayofDarceys = ArrayofGradients*((radius^2)*pi)*K
Sign up to request clarification or add additional context in comments.

1 Comment

yes, I suspect this is the problem as well. +1 from here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.