How do I assign unique values to a i*j matrix/grid?
Show older comments
I have a grid of i rows and j columns. For example, 3 rows and 5 columns. However, the origin is in the bottom left corner, not the top left corner as for matrices.
I have an equation, b= i + j, which I want to use to assign a unique value to each location on the matrix, not to replace the individual values of the matrix/grid. (the matrix doesn't have values, it is just a rectangle divided into a grid). e.g the bottom left grid square will have the value 2 assigned to it.
I have tried using this code:
m=[1 2 3 4 5]
n=[3 2 1]
for i=n(1):n(length(n))
for j= m(1):m(length(m))
l(i,j)= i + j
end
end
I know this doesn't work, because l hasn't been assigned. I'm not sure how to do this however. I want to be able to type l(1,1) for example, and to be able to see which value has been assigned to this point.
I hope I've expressed this clearly enough. I appreciated any help.
Thank you
Accepted Answer
More Answers (1)
Azzi Abdelmalek
on 23 Aug 2014
Edited: Azzi Abdelmalek
on 23 Aug 2014
m=[1 2 3 4 5]
n=[3 2 1]
kn=numel(n);
km=numel(m);
l=zeros(kn,km); % preallocate
for i=1:numel(n)
for j= 1:numel(m)
l(i,j)= n(i) +m(j);
end
end
%or without for loop
m=[1 2 3 4 5]
n=[3 2 1]
[x,y]=meshgrid(m,n)
l=x+y
Categories
Find more on Vector Spaces and Subspaces 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!