Using for loop to extract data from a matrix

1 回表示 (過去 30 日間)
Brendan Görres
Brendan Görres 2020 年 10 月 3 日
コメント済み: Brendan Görres 2020 年 10 月 3 日
I am trying to etract certain data from a matrix into a single row vector with the length of the vector depending on the number of elements in the atrix.
So for example I extracted successfully the element numbers that represents one building component with the following code:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Assign Elements to part Cavity
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
groupc=strfind(aName,'Cavity');
tf= cellfun('isempty',groupc); %tf gives 1, if cell contains []
for i=1:length(tf) %converts [] to 0
if tf(i)==1
groupc(i)={0};
end
end
groupc=cell2mat(groupc);
combic= [aElements,groupc]; %aElements is defined as a cell array containing all existing Element number of the whole building model
cavity= ones(length(aElements),1);
n=1;
for j=1:length(aElements)
if combic(j,groupc(j)==1)
cavity(n)=combic(j,1);
n=n+1;
end
end
The result "cavity" I get is attached to this question. What I want is the loop to stop, before filling out the rows with "1" and give out a vector that has the length of the number of elements that belong to one part (in this case to the part cavity). How can I do that?
  2 件のコメント
madhan ravi
madhan ravi 2020 年 10 月 3 日
編集済み: madhan ravi 2020 年 10 月 3 日
Your question isn't clear, what ones? You already preallocate cavity with ones? Instead of posting a random picture why not illustrate with a short example with the desired result?
cell_array = {[]; 4646; 3883; 0}
desired_result = % ??
Brendan Görres
Brendan Görres 2020 年 10 月 3 日
the problem was the preallocation. I missed that.

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

採用された回答

Abdolkarim Mohammadi
Abdolkarim Mohammadi 2020 年 10 月 3 日
The break statement terminates the for loop:
if j > numel(cavity)
break
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by