- #1

- 4

- 0

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!!

- MATLAB
- Thread starter feicobain
- Start date

- #1

- 4

- 0

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!!

- #2

MATLABdude

Science Advisor

- 1,655

- 4

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
```

EDIT: Ooops, forgot some parentheses...

- Last Post

- Replies
- 6

- Views
- 12K

- Last Post

- Replies
- 2

- Views
- 6K

- Last Post

- Replies
- 5

- Views
- 3K

- Last Post

- Replies
- 3

- Views
- 802

- Last Post

- Replies
- 2

- Views
- 2K

- Replies
- 2

- Views
- 2K

- Replies
- 1

- Views
- 6K

- Last Post

- Replies
- 3

- Views
- 3K

- Last Post

- Replies
- 2

- Views
- 3K

- Last Post

- Replies
- 1

- Views
- 663