Nested loop problem !

Hey every1.. I am working on arrays and I am finding it quite confusing and slowly getting it.. I want to replace the content of a double array with another double array and I belive since is double I should use a nested loop but I am not sure how it works?? I tried several of ways which is below but the content is not being replaced.. I keep checking it from the matlab work space .. My code is below.. the size is 2x100 and in matlab workspace is two rows.. with my code instead of replacing the content it adds extra row to array b?? I want to start the replace from a(3,1) with b(1,1). a(4,1) with b(2,1) etc then when it finishes this row the second row etc
a(x;y)
b(s,f)
d = 2;
for i=3,j=1:100
a(i,j) = b(i -d,j);
i = i+1;
j = j+1
end
Thanks in advance !!

1 件のコメント

Nathan Greco
Nathan Greco 2011 年 7 月 27 日
Note that if your arrays are size 2x100, then you should be indexing them as "a(1,3) with b(1,1). a(1,4) with b(1,2)". (How can you reach a(4,1) if there are only two rows in a? (Note that indexing goes a(row,column))

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

 採用された回答

Nathan Greco
Nathan Greco 2011 年 7 月 26 日

1 投票

Will this do what you are looking for:
EDIT:
a(:,3:end) = b(:,1:end-2);
OR, if you really wanted a for loop:
d = 2;
for i=1:100-d
a(:,i+2) = b(:,i);
end
-Nathan

7 件のコメント

Susan
Susan 2011 年 7 月 27 日
It cause an error?
Nathan Greco
Nathan Greco 2011 年 7 月 27 日
Rather than just saying that it caused an error, please provide what the error was.
Susan
Susan 2011 年 7 月 27 日
The size hasn't change this time but I got this error now ??? Subscripted assignment dimension mismatch.
Error in ==> test2 at 91
a(3:end,:) = b(1:end-1,:);
I don't know if you meant your line with no for loop or with but tried both ways and got the same error..
I did this for loop too for i=3:100 %put the right number here.
for j=1:100
a(3:end,:) = b(1:end-1,:);
end
end
Both ways didnt work !!
Nathan Greco
Nathan Greco 2011 年 7 月 27 日
My current line of code (a(:,3:end) = b(:,1:end-2);) should do the trick, assuming that I understand what you are looking for now.
Susan
Susan 2011 年 7 月 27 日
I tried that line again on its own and its not working .it produced this error ??? Subscripted assignment dimension mismatch.
Nathan Greco
Nathan Greco 2011 年 7 月 27 日
Based on "test" data that I am using that just use the dimensions you provided (2x100), my method seems to work. I used a = ones(2,100); and b = rand(2,100);
The result from a(:,3:end) = b(:,1:end-2); (this line alone, no for loop involved) turns a into a 2x100 (same size) array, with a(1:2,1:2) remaining ones, and the rest of a having been replaced with b.
Susan
Susan 2011 年 7 月 27 日
Yeah, It worked perfectly fine.. was looking at the columns but read your comment that it starts with rows.. Thanks alot for your help.. Now I can carry on and learning more about the loops and arrays :)

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2011 年 7 月 27 日

1 投票

eg:
a = randi(15,4,10) % array 4x10
b = randi(20,2,10) %array 2x10
a(3:end,:)=b

8 件のコメント

Nathan Greco
Nathan Greco 2011 年 7 月 27 日
Note that this will not work if a is smaller than b. I assume that she meant her "a" matrix was size 2x100, but I could be wrong.
Susan
Susan 2011 年 7 月 27 日
hey.. at the begging they both size 2x100 but as soon I add the for loop provided I get the second array as 4x100?? and get this error
??? Attempted to access Signal2(3,1); index out of bounds because size(Signal2)=[2,100].
Error in ==> test2 at 91
Signal1(i,j)=Signal2(i-d,j);
Any idea whats wrong?
Susan
Susan 2011 年 7 月 27 日
I mean first array become 4x100 if it makes any difference and it addes some data to the first array rather than replacing the old entries???
Nathan Greco
Nathan Greco 2011 年 7 月 27 日
What are the sizes of "a" and "b", and what size do you expect "a" and "b" to be after your loop?
Susan
Susan 2011 年 7 月 27 日
a and b are both before the loop 2x100 and I expect the content of b[1,1] will replace the current content of a[3,1] then next loop to replace a[4,1] will take the content of b[2,1] then a[5,1] with b[3,1] until i reaches 100 and etc.. Note I started from i= 3 because I want to keep the first two entries NaN so basically after the loop size of array should not change rather than entries should be copied from second array to first !!
Nathan Greco
Nathan Greco 2011 年 7 月 27 日
Does my edited answer solve your problem? From what I read, it sounds like you want to fill "a" in with values from "b", starting from the 3rd row of "a", leaving out the last two rows from "b".
Susan
Susan 2011 年 7 月 27 日
Yeah thats exactly what I would like to do!!
Nathan Greco
Nathan Greco 2011 年 7 月 27 日
And, sorry for mixing up terminology, the "rows" should actually be "columns".

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

カテゴリ

ヘルプ センター および 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