Matlab, for loop simple division problem

In summary, the conversation discusses an issue with a script that is meant to calculate a result but is producing unexpected output. The issue is identified as a problem with the syntax and variable initialization, and a corrected version of the code is provided.
  • #1
feicobain
4
0
I put down a script like this

for u=(10:10:20)'
i=(1:size(u,1))'
X=zeros(size(u,1),1)
X(i,1)=100/u(i,1)
end

I expect to get a result like

X=
10
5

but it came out like

X=
0
5

It seems it does work if it contain / in the equation. Please help!
 
Physics news on Phys.org
  • #2
Did you realize that i contains two elements? The syntax u(i,1) makes no sense when i contains two elements. You've either got u and i jumbled up, or I'm not understanding what you're trying to do.

You also need to initialize your variables outside the loop.

So, this is probably the code you're looking for:

Code:
u=(10:10:20)'
X=zeros(size(u,1), 1)

for i = 1:size(u,1)
X(i,1) = 100 / u(i,1)
end

Which produces the desired results.

EDIT: Ooops, forgot some parentheses...
 
  • #3


Thank you for sharing your script and the expected result. From a first glance, it looks like your for loop and the division operation are correct. However, the issue may lie in the way you are defining the variable "u". In your for loop, you have specified "u=(10:10:20)'", which creates a vector with only two elements (10 and 20). This means that the for loop will only iterate twice, resulting in a "X" vector with only two values.

To get the desired result, you can define "u" as a vector with more elements, such as "u=(10:10:30)'". This will create a vector with three elements (10, 20, and 30), and your for loop will iterate three times, resulting in a "X" vector with three values. Alternatively, you can also use the "size" function to dynamically determine the size of "u" and use that in your for loop, instead of specifying a fixed value.

I hope this helps and solves the issue you were facing. If you require further assistance, please let me know. Keep up the great work with your Matlab programming!
 

1. What is a for loop in Matlab?

A for loop is a control flow statement that allows you to execute a block of code repeatedly based on a certain condition or a set number of iterations. In Matlab, the syntax for a for loop is for i = 1:n, where i is the loop variable and n is the number of iterations.

2. How do I perform simple division in Matlab using a for loop?

To perform simple division using a for loop in Matlab, you can use the division operator / inside the loop. For example, if you want to divide all elements in an array A by a constant c, you can use the following code:

for i = 1:length(A)
A(i) = A(i)/c;
end

3. Can I use a for loop to divide elements in two different arrays in Matlab?

Yes, you can use a for loop to divide elements in two different arrays in Matlab. You can use the loop variable to index the elements in both arrays and perform the division operation. For example:

for i = 1:length(A)
B(i) = A(i)/C(i);
end

4. How can I optimize my for loop for faster execution in Matlab?

One way to optimize your for loop for faster execution in Matlab is to preallocate the output array before the loop. This can be done by initializing the array with zeros or using the zeros() function. This will reduce the time needed to resize the array during each iteration.

Another way is to vectorize your code, where possible, by using built-in functions and avoiding unnecessary loops. This can significantly improve the performance of your code.

5. Is there an alternative to using a for loop for simple division in Matlab?

Yes, there are alternative ways to perform simple division in Matlab without using a for loop. One option is to use the element-wise division operator ./, which can divide corresponding elements in two arrays without the need for a loop. Another option is to use the arrayfun() function, which can apply a function to each element in an array without using a for loop.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
126
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
570
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
862
Back
Top