MATLAB Answers

Index a 2D matrix from a 2D matrix

3 views (last 30 days)
BBB
BBB on 12 Nov 2019
Commented: Matt J on 12 Nov 2019
Hi,
I have a 600x1000 dim 2D matrix and I want to use a nested for loop to index out a 51x51 matrix out of it. How can we do that? It has to be in a nested loop format of--
for ii = 1:1000
for jj = 1:600
...
Thanks.

Answers (1)

Arash Rabbani
Arash Rabbani on 12 Nov 2019
Well you can simply do this without a loop:
A=rand(600,1000);
B=A(1:51,1:51);
But if you need it to use loops
B=zeros(51);
for ii = 1:51
for jj = 1:51
B(ii,jj)=A(ii,jj);
end
end

  3 Comments

BBB
BBB on 12 Nov 2019
Hi,
Thank you for your response!
I actually tried your loop and the other non-loop suggestion, but the moment I put in my data set instead of rand, it gives me an error saying -- " Index in position 1 exceeds array bounds (must not exceed 1)."
So basically, I have to do something like this
for hh = 1:1000
for pp = 1:600
b(hh,pp) = lk.*filt; %lk is the original 600x1000 2D data and filt here is another 2D 51x51 2D matrix that I need to multiply the 51x51 chuncks of data from lk.
x_side = hh(lk).*filt;
y_side = pp(im).*filt;
end
end
Walter Roberson
Walter Roberson on 12 Nov 2019
Perhaps you should be looking at conv2() ?
Matt J
Matt J on 12 Nov 2019
Perhaps you should be looking at conv2() ?
As I also mentioned in your duplicate post here,

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!