I need help with my While Loop!
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Uma Dixit
am 23 Feb. 2020
Kommentiert: darova
am 26 Feb. 2020
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 Kommentare
darova
am 26 Feb. 2020
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
Akzeptierte Antwort
Jakob B. Nielsen
am 24 Feb. 2020
Bearbeitet: Jakob B. Nielsen
am 24 Feb. 2020
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!