Im trying to find the absolute value of the difference between current value 'c' and the values 'n'.
3 views (last 30 days)
Show older comments
Paris Mayberry
on 15 Sep 2017
Commented: Paris Mayberry
on 15 Sep 2017
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?
0 Comments
Accepted Answer
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
0 Comments
More Answers (1)
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.
See Also
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!