1

I have an array S 440x120 in dimensions. There is another array T 440x1.

I need to append say rows of S to another array T1 based on the following conditions

  1. The rows appended must be from index say 100 to 200 in S.
  2. Only those rows with T(100:200)==1 should be included. This means that if T(100)=0 then S(100,:) should not be included and if T(101)=1 then S(101,:) should be included.

I tried using the following but it failed due to obvious reasons as it includes from the first index and not from the 100th.

T1=S(T(100:200)==1,:);

Is there a shorter way of doing this in matlab apart from writing a whole loop? Thanks for your answers.

2
  • When you say "add all columns of S to another array", do you mean add each column of S or add the sum of all columns of S? Commented Nov 13, 2013 at 9:56
  • Each column of S is what I mean. Commented Nov 13, 2013 at 9:57

3 Answers 3

2

You almost have it. You only need to add an offset to the numeric (not logical) indices:

N = 100;
M = 200;
result = S(N-1+find(T(N:M)==1),:);
Sign up to request clarification or add additional context in comments.

5 Comments

Uh Oh! Ill try that. Could you please comment the code so I can understand what it actually does? :P
Thanks! Why the sum though? I think I got my answer from this though. Will try it out.
@SohaibI You said "add those colummns"... by "add" did you mean "sum" or simply "include"?
I don't mean add as in addition! Ill make the edit in my question. I mean add as in append to that.
I have already written that as an answer in my question. This does not work. It includes rows from the first index in S and not from the 100th index.
2

How about T1=S([zeros(99,1); T(100:200)]==1,:);?

4 Comments

Not sure if the zeros should be a row or column matrix. Either this or [zeros(1,99) T(100:200)]. You use the zeros to ignore the first 99 elements of S and start at index 100.
it should be 101. Ill try it out and report back.
Subscript indices must either be real positive integers or logicals?
@SohaibI Make sure you don't have a typo, it seems to work fine.
1

Here you go:

idx = find(T==1);
idx = idx(idx>=100 & idx<=200);

S(idx,:)

3 Comments

You could save the first 99 ==1 comparisons... :-)
Can't mark all answers correct unfortunately :P @LuisMendo I think you took that find() function idea from here ;) Thanks for all the help.
@SohaibI Not really :-) When you want to add an offset to a set of indices, the indices have to be in numeric form, not in logical form; and find does just that. What took me some time was to understand the question ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.