An indexing expression on the left side of an assignment must have at least one subscript. ???

13 views (last 30 days)
Hi! I keep getting the error: "An indexing expression on the left side of an assignment must have at least one subscript." on my code:
function myMultProd=x(v)
n_vec()=zeros(1, length(x));
for i=1:length(x)
n=x(i);
for j=1:i
n=x(j)*n;
end
n_vec(i)=n;
end
print n_vec;
end
Can someone help me pleaaaase? :)

Answers (1)

Walter Roberson
Walter Roberson on 15 Jan 2019
Edited: Walter Roberson on 15 Jan 2019
n_vec = zeros(1, length(x));
By the way: are you sure that you want x(i) to be multiplied twice in the expression? Once when you initialize n=x(i) and once when j=1:i reaches j=i ?
  2 Comments
Dayana Lujan
Dayana Lujan on 15 Jan 2019
The n_vec does not work without the parantheses like you proposed :(. and I think I have to multiply twice because the hw says to:
"Write a function called myMultProd.m that computes the cumulative product of the elements in a vector. The cumulative product, pj, of the jth element of the vector x, xj,
is defined by
pj = (x1)(x2) ... (xj)
for j = 1:length of the vector x. For example, when you run your function it should look like this:
>> x = [2 3 4 2]; >> myMultProd(x) >> ans =
2 6 24 48
That is, the function returns: 2, 2*3, 2*3*4, 2*3*4*2 is p3, etc.
where 2 is p1, 2*3 is p2, 2*3*4"
Walter Roberson
Walter Roberson on 15 Jan 2019
function pj = myMultProd(x)
You should have another look at your code. When i = 4, then you would do
n <- x(4) which is n <- 2
j <- 1
n <- x(1) * n which is n <- 2 * 2 which is n <- 4
j <- 2
n <- x(2) * n which is n <- 3 * 4 which is n <- 12
j <- 3
n <- x(3) * n which is n <- 4 * 12 which is n <- 48
j <- 4
n <- x(4) * n which is n <- 2 * 48 which is n <- 96

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!