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
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))
采纳的回答
更多回答(1 个)
Andrei Bobrov
2011-7-27
eg:
a = randi(15,4,10) % array 4x10
b = randi(20,2,10) %array 2x10
a(3:end,:)=b
8 个评论
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
2011-7-27
Susan
2011-7-27
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
2011-7-27
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
2011-7-27
Nathan Greco
2011-7-27
And, sorry for mixing up terminology, the "rows" should actually be "columns".
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!