Sorting Elements of af 3D matrix in another Matrix with a condition

1 回表示 (過去 30 日間)
Alex Perrakis
Alex Perrakis 2021 年 9 月 7 日
回答済み: Salman Ahmed 2021 年 10 月 14 日
x1=0.05;
x2=0.1;
B=zeros(428,29,2);
k=1;
i=1;
for j=1:29; %columns
if x2>E_D(i,j,1)& E_D(i,j,1)>x1;
B(i,k,:)=E_D(i,j,:);
end
x2=x2+0.05;
x1=x1+0.05;
i=1+1;
k=k+1;
if i>428
break
end
end
I have written this code that should search the first sheet of a 3D Matrix, then apply the condition and place the elements of the first and second sheet of the cell in another Matrix of the same size and the column should change with the loop iteration, but it does not seem to work, it gives a Matrix B of Zeros.
  1 件のコメント
David Goodmanson
David Goodmanson 2021 年 9 月 7 日
Hi Alex,
I tried setting E_D to a random matrix and it does occasionally come up with a nonzero element for B. However, your i>428 statement implies that you expect i to eventually hit that value. But you have
i = 1+1 rather than i = i+1
so i is always 2. Even after making that change, i is never larger than 29. So you may have some work to do with the code.

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

回答 (1 件)

Salman Ahmed
Salman Ahmed 2021 年 10 月 14 日
Hi Alex,
From what I observe from your code is that you would like to iterate over and sort elements based on the condition defined using matrix E_D, x1 and x2. Assuming you want to execute the j loop for all possible values of i, consider the following modification to your code. Hope it helps.
x1=0.05;
x2=0.1;
B=zeros(428,29,2);
E_D=rand(428,29,2); % Substitute your E_D matrix
k=1;
i=1;
while(1) % Add this to loop over values of i
for j=1:29 %columns
if x2>E_D(i,j,1)&&E_D(i,j,1)>x1
B(i,k,:)=E_D(i,j,:);
x2=x2+0.05;
x1=x1+0.05;
end
end
i=i+1; % Keep the increments outside j loop
k=k+1;
if i>=428
break
end
end

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by