Im trying to find the absolute value of the difference between current value 'c' and the values 'n'.

3 views (last 30 days)
Im trying to find the absolute value of the difference between current value 'c' and the values 'n'. Matlab keeps coming back saying 'Subscripted assignment dimension mismatch.'
E=[363725;142414;795692;184315];
c=E(3,1);
n = [4;9;8]
for i=1:length(n)
N(1,i) = abs(c-n(1,i));
end
What is wrong with the second line in my for loop?

Accepted Answer

Image Analyst
Image Analyst on 15 Sep 2017
Edited: Image Analyst on 15 Sep 2017
You say n(1,i), meaning the 3rd column of the first row. However, n is a column vector with only one column, not 3. Don't use 1, or ,1 when dealing with row or column vectors - it's not needed and just complicates things.
The fix:
E=[363725;142414;795692;184315]
c=E(3)
n = [4;9;8]
for i=1:length(n)
N(i) = abs(c-n(i))
end

More Answers (1)

James Tursa
James Tursa on 15 Sep 2017
Edited: James Tursa on 15 Sep 2017
Change n(1,i) to n(i). n is a column vector but you are indexing into it as if it were a row vector. You could use n(i,1), but n(i) is simpler for vector indexing. You might want to change the N indexing as well.
  1 Comment
Paris Mayberry
Paris Mayberry on 15 Sep 2017
thanks, although i changed that it still didnt work. this loop is within a function and an input to the function is c=[3,1], i think it isnt working because c needs to be changed from the position to the actual element with in this case is 7(from the array E) but im not sure of how to do this?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!