필터 지우기
필터 지우기

Index a v dimensional matrix from a matrix with v columns and n rows

조회 수: 1 (최근 30일)
Jpk
Jpk 2020년 9월 30일
편집: madhan ravi 2020년 9월 30일
Hello all,
I a have u row and n column matrix, say p, in which I want to store some data. I also have a matrix ind which has per each row the indices that I need in order to write to.
For example let me se the dimension to n=2 and supposing I want to write to position ind(1,:) of P I have:
ind = [1,2;2,6];
p(sub2ind([size(p,1),size(p,2)],ind(1,1),ind(1,1))) = datum; % Ican easly loop to handle more data
While this works I am not clear how to address the possibililty of having a 50 column (i.e. n=50) ind matrix as the prototype of subs2ind requires me to specify the indexes on by one.
Does anyone know how to deal with such scenario?

답변 (1개)

madhan ravi
madhan ravi 2020년 9월 30일
p(sub2ind(size(p), ind(:, 1), ind(:, 2)) = Dayum % where RHS should be equal to size(ind)
  댓글 수: 4
Jpk
Jpk 2020년 9월 30일
편집: Jpk 2020년 9월 30일
You have to excuse me - I was not paying enough attention both in writing my question or in my reply. I will edit the question soon but allow me to reformulate.
Consider ind a t row and n column matrix of indices.
My goal is to use for each row of ind an index for a specific dimension of p. Of course n is know ahead of time.
eg.
ind = [1,2,4;2,6,5];
p( sub2ind(size(p),ind(1,1),ind(1,2),ind(1,3)) ) = datum; % in this case n=3
if n=4
ind = [1,2,4,5;2,6,5,9];
p( sub2ind(size(p),ind(1,1),ind(1,2),ind(1,3),ind(1,4)) ) = datum; % in this case n=4
In other words the kth colum of ind contains the indices for the kth dimension of p.
madhan ravi
madhan ravi 2020년 9월 30일
편집: madhan ravi 2020년 9월 30일
for k = 1 : size(ind, 1)
I = num2cell(ind(k, :))
p(sub2ind(size(p), I{ : }))= datum;
end

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

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by