I need help with my While Loop!

3 ビュー (過去 30 日間)
Uma Dixit
Uma Dixit 2020 年 2 月 23 日
コメント済み: darova 2020 年 2 月 26 日
I have an original array (3D) called 'pixelArray' that I eventually need to crop 3 different ways. So I have 3 different sets of x1,x2,y1,y2 numbers that crop the original array in different ways.
I currently have it set up like this:
x1 = [52, 52, 52];
x2 = [480, 480, 480];
y1 = [4, 1, 205] ;
y2 = [392, 205, 392];
n = 1;
while n < 3
%crop rows
pixelArray(1:y1(n),:,:)=[];
pixelArray(y2(n):end,:,:) = [];
%crop columns
pixelArray(:,1:x1(n),:)=[];
pixelArray(:,x2(n):end,:)=[];
n =n+1;
end
But I want to save each iteration's new pixelArray as its own matrix. What is the best way to go about doing this?
  3 件のコメント
Uma Dixit
Uma Dixit 2020 年 2 月 26 日
Oh, very good point. How do you suggest I make sure the original pixelArray matrix stays intact in its dimensions?
darova
darova 2020 年 2 月 26 日
Remove elements from the end?
>> a = 1:10
a =
Columns 1 through 9
1 2 3 4 5 6 7 8 9
Column 10
10
>> a(7:9) = []
a =
1 2 3 4 5 6 10
>> a(1:3) = []
a =
4 5 6 10

サインインしてコメントする。

採用された回答

Jakob B. Nielsen
Jakob B. Nielsen 2020 年 2 月 24 日
編集済み: Jakob B. Nielsen 2020 年 2 月 24 日
Index them! Either as a structure, or an array with a 4th dimension. I personally favour structures, since 4D data makes my head hurt ;) dot indexing is great for looping results.
Also note that you will only get 2 runs of your loop, as after the 2nd run n will equal 3, and your loop terminates before running a 3rd time since the condition is less than 3. Either make the condition less than or equal to 3, or you can use a for loop instead, as your while loop is essentially a for loop where you do the counting yourself - something you dont need to do in this case.
for n=1:3
%crop rows
pixelArray(1:y1(n),:,:)=[];
pixelArray(y2(n):end,:,:) = [];
%crop columns
pixelArray(:,1:x1(n),:)=[];
pixelArray(:,x2(n):end,:)=[];
Crops(n).pixelArray=pixelArray;
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by