Storage elements from matrix in other one, with indexing.
Show older comments
I want to storage information from one matrix to another. In this example, from A matrix I have to extract to storage in B matrix. The solution should be:
B = [6 4
6 8]
I tried so hard, I don't get it.
Please help me.
A = [
8 3 0 2
9 6 1 4
10 6 2 1
1 6 2 8
];
a=2;
b=4;
B = zeros(2)
m=1
while m<=2
k= 1
while k<=2
i=1
while i<=4
if i==a
n1 = i
if n1<=2
j = 1
while j<=4
if j==b
n2=j
if n2<=2
B(m,k) = B(m,k)+A(n1,n2)
%B(m,k) = B(m,k)+A(n1,n2)
end
end
j = j+1
end
end
end
i = i+1
end
k = k+1
end
m=m+1
end
Accepted Answer
More Answers (1)
The MATLAB approach:
A = [8,3,0,2;9,6,1,4;10,6,2,1;1,6,2,8]
v = [2,4];
out = A(v,v)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!