Get the position of the next in row and next in column in a matrix

15 visualizzazioni (ultimi 30 giorni)
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 Commenti
Stephen23
Stephen23 il 12 Ott 2021
Modificato: Stephen23 il 12 Ott 2021
@Image Analyst: I am asking about Index 9, whose "Next in Row" value 10 appears that it should be 0.
What is the next non-zero value on the 4th row, after the 3rd column?
The "Next" values for Index 10 appear to be correct.
De Silva
De Silva il 12 Ott 2021
yes you are correct @Stephen for index 9, next in row =0 and ,next in column = 0;
Thank you

Accedi per commentare.

Risposte (2)

Stephen23
Stephen23 il 12 Ott 2021
Modificato: Stephen23 il 12 Ott 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]
A = 5×5
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)
1 1 1 1 2 3 2 -2 1 3 0 6 3 2 2 1 4 10 4 8 2 2 5 8 5 1 2 4 0 0 6 3 3 3 7 9 7 -2 3 5 0 12 8 -3 4 2 9 11 9 2 4 3 0 0 10 1 5 1 11 0 11 2 5 2 12 0 12 -4 5 5 0 0
  5 Commenti
Stephen23
Stephen23 il 12 Ott 2021
Modificato: Stephen23 il 12 Ott 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
A = 5×5
1 0 -2 0 0 2 8 0 1 0 0 0 3 2 -2 0 -3 2 0 0 1 2 0 0 -4
% 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)
1 1 1 1 2 3 2 -2 1 3 0 6 3 2 2 1 4 10 4 8 2 2 5 8 5 1 2 4 0 13 6 3 3 3 13 9 7 -2 3 5 0 12 8 -3 4 2 9 11 9 2 4 3 0 0 10 1 5 1 11 0 11 2 5 2 12 0 12 -4 5 5 0 0 13 2 3 4 7 0

Accedi per commentare.


David Hill
David Hill il 9 Ott 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 Commenti
kalana agampodi
kalana agampodi il 11 Ott 2021
Could you please show the results if the above code works for you ?
Thank you
David Hill
David Hill il 12 Ott 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.

Accedi per commentare.

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by