create 1/n from 1 to 20 without using loop in matlab

To create a vector of 1/n from 1 to 20 without using a loop in Matlab, you can use vectorization. You can achieve this by dividing 1 by a vector containing the numbers from 1 to 20.

Here's the code:

main.m
n = 1:20;
result = 1./n;
25 chars
3 lines

The n variable represents a vector containing the numbers from 1 to 20. The ./ operator performs element-wise division between 1 and each element in the vector n, generating a new vector result.

This vector result will contain the values of 1/n from 1 to 20 without using a loop.

related categories

gistlibby LogSnag