Get the position of the next in row and next in column in a matrix
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I want to get the next in row and next in column in the above matrix.
Index column represent the all non zero entries in the matrix.
For an example if you are at A(1,1) it shoud give me the index for -2 from the index colum and update next in row to 2. Because -2 is index 2 in the index column. For next in colum at A(1,1) it should get the index from index column for 2 so the next in column will be 3.
When you are at A(2,1). The next in row is 8. this should give me 4 from index column (because 8 is the 4th non zero entry in the matrix), for next in column it should give me 10 from index colum because 1 is the 10th non zero entry of the matrix.
Please give me a hint
12 comentarios
Respuestas (2)
Stephen23
el 12 de Oct. de 2021
Editada: Stephen23
el 12 de Oct. de 2021
A reasonably simple, efficient, robust approach:
A = [1,0,-2,0,0;2,8,0,1,0;0,0,3,0,-2;0,-3,2,0,0;1,2,0,0,-4]
[C,R] = find(A.');
N = numel(R);
M = repmat([R,C],1,3);
M(:,1) = 1:N;
M(:,2) = nonzeros(A.');
M(:,5:6) = 0;
for k = 1:N
X = find(C>C(k) & R==R(k),1);
Y = find(R>R(k) & C==C(k),1);
if numel(X)
M(k,5) = M(X,1);
end
if numel(Y)
M(k,6) = M(Y,1);
end
end
disp(M)
5 comentarios
Stephen23
el 12 de Oct. de 2021
Editada: Stephen23
el 12 de Oct. de 2021
So you want to specify which index the value has and not just get the code to implicitly determine the index.
That does not match what you asked (or implied) for in your original question: adding new requirements might mean fundamental changes... lets give it a try and see what we can do:
% Run once at the start:
A = [1,0,-2,0,0;2,8,0,1,0;0,0,3,0,-2;0,-3,2,0,0;1,2,0,0,-4];
[C,R] = find(A.');
N = numel(R);
M = repmat([R,C],1,3);
M(:,1) = 1:N;
M(:,2) = nonzeros(A.');
M(:,5:6) = 0;
% Repeat as required: add new data point/s to the end of the existing data:
M(end+1,1) = M(end,1)+1; % index
M(end,2) = 2; % value
M(end,3) = 3; % row
M(end,4) = 4; % column
A(3,4) = 2 % optional, not required by this code
% Repeat as required:
M = sortrows(M,[3,4]);
for k = 1:size(M,1)
X = find(M(:,4)>M(k,4) & M(:,3)==M(k,3),1);
Y = find(M(:,3)>M(k,3) & M(:,4)==M(k,4),1);
if numel(X)
M(k,5) = M(X,1);
end
if numel(Y)
M(k,6) = M(Y,1);
end
end
M = sortrows(M,1);
disp(M)
David Hill
el 9 de Oct. de 2021
matlab indexing is down and then across.
A=[1 0 -2 0 0;2 8 0 1 0;0 0 3 0 -2;0 -3 2 0 0;1 2 0 0 -4];
a=A';%gets your indexing
a(a~=0)%shows your indexing for each non-zero in A
Recommend using linear indexing if you can.
%if you are at a(1), then the next row/column (will be reverse yours)
N=1;%current location
[m,n]=size(a);
if mod(N,m)~=0
b=ceil((N+1)/m);
f=find(a(N+1:b*m),1);
if ~isempty(f)
r=mod(N,m)+f;
else
r=[];
end
else
r=[];
end
if N<=m*(n-1)
f=find(a(N+m:m:m*n),1);
if ~isempty(f)
c=ceil(N/m)+f;
else
c=[];
end
else
c=[];
end
9 comentarios
David Hill
el 12 de Oct. de 2021
I am using linear indexing. A(1,1)==A(1).
a=[1 0 -2 0 0;2 8 0 1 0;0 0 3 0 -2;0 -3 2 0 0;1 2 0 0 -4];
N=1;%current location, linear indexing into A
[m,n]=size(a);
if mod(N,m)~=0
b=ceil((N+1)/m)
f=find(a(N+1:b*m),1)
if ~isempty(f)
nextRowNumber=mod(N,m)+f
else
nextRowNumber=[]
end
else
nextRowNumber=[]
end
if N<=m*(n-1)
f=find(a(N+m:m:m*n),1)
if ~isempty(f)
nextColumnNumber=ceil(N/m)+f
else
nextColumnNumber=[]
end
else
nextColumnNumber=[]
end
Output for A(1), nextColumnNumber=3, nextRowNumber=2
Output for A(7). nextColumnNumber=4, nextRowNumber=4
If you don't like linear indexing you can convert from sub2ind()
sub2ind(size(a),2,2);%which equals 7
I don't plan on responding again.
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!