How do I assign unique values to a i*j matrix/grid?

조회 수: 6 (최근 30일)
Sarah
Sarah 2014년 8월 23일
댓글: Sarah 2014년 8월 25일
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

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 8월 23일
Sarah - the problem is in the outer for loop
for i=n(1):n(length(n))
Since initial value (initval) is n(1)=3, end value (endval) is n(length(n))=1, and index is i, the loop becomes
for i=3:1
which from for description states that increments the index variable from initval to endval by 1, and repeats execution of program statements until index is greater than endval.
Since the index immediately greater than the end value (3>1) we don't enter the for loop. I think what you want to do is something like the following
% pre-allocate memory to the output matrix
output = zeros(length(n),length(m));
for u=n
for v=m
output(u,v) = u + v;
end
end
The above allows us to iterate with the indices u and v (I switched these from your i and j since MATLAB uses these two to (also) represent the imaginary number) over each element in n and m respectively. Basically, the statement for u=n allows us to let u be each element in n. The output from the above is
output =
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
which is not quite what you wanted since you stated that the the origin is in the bottom left corner, not the top left corner as for matrices.
So we can do this transformation as follows
numRows = length(n);
numCols = length(m);
% pre-allocate memory to the output matrix
output = zeros(numRows,numCols);
for u=n
for v=m
output(numRows-u+1,numCols-v+1) = u + v;
end
end
The output from the above is
output =
8 7 6 5 4
7 6 5 4 3
6 5 4 3 2
Note that we are assuming that the range in each vector, m and n, is from 1 to the length of the vector.
  댓글 수: 1
Sarah
Sarah 2014년 8월 25일
Thank you! Not exactly what I was looking for but it gave me a few ideas. I really appreciate the response.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 8월 23일
편집: Azzi Abdelmalek 2014년 8월 23일
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
  댓글 수: 1
Sarah
Sarah 2014년 8월 25일
Thank you very much! I appreciate the response.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Vector Spaces and Subspaces에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by