0

I am a newbie to Matlab.Still trying to learn the syntax and everything.I have a large data matrix D.I am trying to remove part of the large matrix and make small matrices.I am searching the 3 columns and returning matrices which contain the numbers in the 3 columns.

BPI20 = D(D(:,1) == 0 &D(:,2)==0 &D(:,6)==20,:); BPI21 = D(D(:,1) == 0 &D(:,2)==0 &D(:,6)==21,:); BPI22 = D(D(:,1) == 0 &D(:,2)==0 &D(:,6)==22,:); BPI23 = D(D(:,1) == 0 &D(:,2)==0 &D(:,6)==23,:); BPI24 = D(D(:,1) == 0 &D(:,2)==0 &D(:,6)==24,:); 

I want to write a for loop for the above code where the value of the variable BPI should also increase Thank you

9
  • What does D(:,6 look like? 1:24? Commented Sep 25, 2012 at 16:42
  • D(:,6)=1:24.It is 1 for a certain number of rows and increments by 1 Commented Sep 25, 2012 at 16:50
  • and what should be in BPI18 or BPI{18} if D(18,1)~=0 or D(18,2)~=0? zeros? NaNs? empty? What do you want to do with it aftwerwards? Because you could potentially just simplify all your code above to: BPI = D(D(:,1)==0 & D(:,2)==0) Commented Sep 26, 2012 at 6:05
  • But I want separate matrices for BPI 20 to BPI 24.So I want to write for loop for the code above Commented Sep 26, 2012 at 14:20
  • ok, then use the loop in my solution and use BPI{1}, BPI{2}, etc instead of BPI1, BPI2, etc. Can you still tell us what you want to do with the resulting data? You know some BPI{ii} will be empty right? If D(ii,1) == 0 &D(ii,2)==0 is not satisfied, BPI{ii} will be empty!! That's not due to my code, it's also present in the code in your question.. Commented Sep 26, 2012 at 16:08

1 Answer 1

1

Do not use such a name for BPI (ie names with an increment in the name itself), use a cell array instead:

col1is0 = D(:,1)==0; col2is0 = D(:,2)==0; maxBPI = 24; BPI = cell(maxBPI,1); for ii=1:maxBPI BPI{ii} = D(col1is0 & col2is0 & D(:,6)==ii, :); end 

see also this post!

Sign up to request clarification or add additional context in comments.

4 Comments

But will I have to convert the cell to matrix again?Because D is a matrix.
How can I access the cell arrays?And I need to do operations on the final BPI.So I will have to convert it to a matrix.I need to plot data from BPI.
my previous comment was wrong, or at least won't work always. click on the link in my post to learn how to work with cell arrays, you can see what's in a cell by using BPI{2} for example.
D(:,6)=1:24.It is 1 for a certain number of rows and increments by 1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.