How to map elements?

조회 수: 4 (최근 30일)
Rui
Rui 2013년 1월 9일
Hello everybody,
I am cleaning some code and I came across with the following and I hope you guys can give some extra insigths.
The code is working like shown but I'd like to simplify it somehow.
  • Both hil and rom are allocated.
  • schema contains indexes. The value of these indexes is no greater than the dimensions of hil and rom.
  • hil initially is zeros and I want to fulfill it with values from rom in due position.
for i = 1:M
for j = 1:N
hil(i,schema(i,:,j),j) = rom(i,schema(i,:,j),j);
end
end
Thanks,
Rui

채택된 답변

Matt J
Matt J 2013년 1월 9일
편집: Matt J 2013년 1월 9일
I think your current approach is optimum.
The only exception is if you're going to repeat this for different rom, but the same schema. Then I would do
[ii,jj]=ndgrid(1:N);
ss=scheme(1:N,:,1:N);
idx=sub2ind(size(rom),ii(:),ss(:),jj(:) );
and now you can repeatedly do
hil(idx)=rom(idx)
reusing idx for different rom.

추가 답변 (1개)

Rui
Rui 2013년 1월 10일
Thanks Matt. In my case I am not able to redefine the number of elements on any dimension (if just 1 dimension could be redimensioned, we could get rid off 1 cycle, and so on), so I suppose I will have to live with that.
Cheers, Rui

카테고리

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