Trying to index an array and extract its values with a for loop.

조회 수: 29 (최근 30일)
Raven Buckman
Raven Buckman 2022년 1월 11일
댓글: Ive J 2022년 1월 11일
I have an table of probabilities and I want to index the table to determine if there is a probability in any of the three columns that is not 1 or 0, extract the row and put it into a new array. I'm fairly new to working with matlab so I'm sure there is a very simple way to do this but it is evading me. Any help would be great.
i = 1;
for scrs(i,:) ~= 1 || scrs(i,:) ~= 0
unclassifiable = scrs(i,:);
i = i+1;
disp('Unclassifiable nanoparticles detected.')
end

채택된 답변

Cris LaPierre
Cris LaPierre 2022년 1월 11일
편집: Cris LaPierre 2022년 1월 11일
See MATLAB Onramp Ch 5: Indexing and Modifying Arrays.
You might also find Ch 12: Programming helpful since it covers for loops.
For loops start with a declaration of a counter, which is automatically incremented each loop, and repeats a fixed number of times. Note that this approach assumes scrs only has 1 column of data. If that is not true, you will need to modify this approach.
for i = 1:size(scrs,2)
if scrs(i,:) ~= 1 || scrs(i,:) ~= 0
unclassifiable(i,:) = scrs(i,:);
disp('Unclassifiable nanoparticles detected.')
end
end
What you have written looks more like a while loop to me, which continue to loop as long as the conditional expression is true. However, this stops looping the first time scrs is 1 or 0. Note that this approach assumes scrs only has 1 column of data. If that is not true, you will need to modify this approach.
i = 1;
while scrs(i,:) ~= 1 || scrs(i,:) ~= 0
unclassifiable(i,:) = scrs(i,:);
i = i+1;
disp('Unclassifiable nanoparticles detected.')
end
Not sure which one you want so try them both to understand the difference.
  댓글 수: 1
Raven Buckman
Raven Buckman 2022년 1월 11일
Thanks so much. I want it to keep looping throughout the entire matrix so this is quite helpful. Thank you.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Kevin Holly
Kevin Holly 2022년 1월 11일
Below are two possible ways.
If data is in table,
t=table;
t.column1 = [1 2 3 4 0 3 4 2 3 4 0 3 1]';
t.column2 = [8 7 9 0 3 9 2 1 2 3 1 0 3]';
t.column3 = [9 0 9 7 2 1 8 3 9 2 3 0 1]';
t
t = 13×3 table
column1 column2 column3 _______ _______ _______ 1 8 9 2 7 0 3 9 9 4 0 7 0 3 2 3 9 1 4 2 8 2 1 3 3 2 9 4 3 2 0 1 3 3 0 0 1 3 1
t((t.column1==0),:)=[];
t((t.column2==0),:)=[];
t((t.column3==0),:)=[];
t((t.column1==1),:)=[];
t((t.column2==1),:)=[];
t((t.column3==1),:)=[];
t
t = 4×3 table
column1 column2 column3 _______ _______ _______ 3 9 9 4 2 8 3 2 9 4 3 2
If data is in matrix,
t = [1 2 3 4 0 3 4 2 3 4 0 3 1;8 7 9 0 3 9 2 1 2 3 1 0 3;9 0 9 7 2 1 8 3 9 2 3 0 1]'
t = 13×3
1 8 9 2 7 0 3 9 9 4 0 7 0 3 2 3 9 1 4 2 8 2 1 3 3 2 9 4 3 2
index = (sum((t~=1).*(t~=0),2)==3)
index = 13×1 logical array
0 0 1 0 0 0 1 0 1 1
t(index,:)
ans = 4×3
3 9 9 4 2 8 3 2 9 4 3 2
  댓글 수: 1
Ive J
Ive J 2022년 1월 11일
The second approach is not efficient; this is way faster:
index = all(t ~= 0 & t ~= 1, 2);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by