Is there a way to index a variable to a certain position in the array

8 ビュー (過去 30 日間)
Ruijie Shao
Ruijie Shao 2018 年 10 月 23 日
コメント済み: Fangjun Jiang 2018 年 10 月 23 日
I have some code that looks like this:
if n==1
for i=1:size(ob,1)
rt(i,:,:)=radon(squeeze(ob(i,:,:)),ra);
end
elseif n==2
for i=1:size(ob,2)
rt(i,:,:)=radon(squeeze(ob(:,i,:)),ra);
end
elseif n==3
for i=1:size(ob,3)
rt(i,:,:)=radon(squeeze(ob(:,:,i)),ra);
end
end
Is there a way to get the i variable to the nth position of the ob matrix so that I don't have to write an if statement for each n?

採用された回答

Fangjun Jiang
Fangjun Jiang 2018 年 10 月 23 日
Depending on the value of n, you want to loop through the different dimension of matrix.
I think you can utilize the function shiftdim() to pre-process you matrix and then do the same loop, something like
shiftdim(ob,n)

その他の回答 (1 件)

Jose Sanchez
Jose Sanchez 2018 年 10 月 23 日
This should work:
n=3; % or set n=1 or n=2
tmp = permute(ob, [setdiff(1:3,n) n]);
for i=1:size(tmp,3)
rt(i,:,:) = radon(tmp(:,:,i),ra);
end

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by