我正在嘗試創建一個回圈,一次從陣列中選擇 3 行,然后再進行一些計算。
例如:
陣列 = [2 3 4; 4 5 6; 7 8 9;10 11 23;23 56 78;67 55 89;90 87 32]
所以在第一個回圈中它應該選擇 [2 3 4; 4 5 6; 7 8 9]
在第二個回圈中 [10 11 23; 23 56 78;67 55 89]。
我正在努力使這成為可能。
uj5u.com熱心網友回復:
希望代碼幫助:
clear, clc
% Array
Arr = [2 3 4; 4 5 6; 7 8 9; 10 11 23; 23 56 78; 67 55 89; 90 87 32];
% Define The Length to Prevent the Error of Iteration in For Loop
if mod(size(Arr,1),3) ==0
LenArr = size(Arr,1); % Length Examined
else
LenArr = size(Arr,1) - mod(size(Arr,1),3);
end
Counter = 1;
for i = 1:3: LenArr
Iter = Arr(i:i 2, :); % Here the Answer of the Question
% Disply Results ----------
fprintf('Iteration %d = \n',Counter)
disp(Iter)
Counter = Counter 1;
%--------------------------
end
結果:
Iteration 1 =
2 3 4
4 5 6
7 8 9
Iteration 2 =
10 11 23
23 56 78
67 55 89
uj5u.com熱心網友回復:
你可以像下面的代碼那樣做
您的演算法可以應用于 selectBlockLocations 變數。
array = [2 3 4; 4 5 6; 7 8 9; 10 11 23; 23 56 78; 67 55 89; 90 87 32];
number_of_loop = floor(length(array)/3);
for i=0:number_of_loop-1
selectBlockLocations = array(i*3 1:i*3 3, 1:end);
disp(selectBlockLocations)
end
結果:
2 3 4
4 5 6
7 8 9
10 11 23
23 56 78
67 55 89
玩的很開心!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/405413.html
標籤:
下一篇:MATLAB回圈重構
