Adding a row vector into the diagonal of another vector with m, n dimensions using for loop.

1 view (last 30 days)
Suppose we have a matrix A that is ones(6,8) and another vector b that is [1:10]
i want to make a new array (Add_array) using b and it should have the exact dimensions as matrix A, but with the elements of b in its diagonal using FOR loop. the length of b and A can differ.
After the new array has been made, i have to add it matrix A which has the same dimensions as the new array to get B.
function B = NewArray(A,b)
%NEWARRAY Summary of this function goes here
% Detailed explanation goes here
[m,n] = size(A)
Rows = m;
Columns = n;
Add_array = zeros(Rows,Columns);
for i = 1:Rows
for j = 1:Columns
Add_array(i,j) = Add_array(i,j) + b(1,j);
end
end
B = [Add_array] + [A]
end
This is the code i have written so far^
ans =
2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
This is the result I am getting.^
ans =
2 1 1 1 1 1 1 1
1 3 1 1 1 1 1 1
1 1 4 1 1 1 1 1
1 1 1 5 1 1 1 1
1 1 1 1 6 1 1 1
1 1 1 1 1 7 1 1
This is the expected result.^
I need help with getting the expected result using FOR loops.
thanks!
  3 Comments
Humza Khan
Humza Khan on 17 Oct 2021
its not incorrect.
But the assignment I am working on demands the use of for loop for this function and i am very confused.
Stephen23
Stephen23 on 17 Oct 2021
A = ones(6,8)
A = 6×8
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
b = 1:10
b = 1×10
1 2 3 4 5 6 7 8 9 10
S = size(A);
B = diag(b);
B(end+1:S(1),end+1:S(2)) = 0;
B = A + B(1:S(1),1:S(2))
B = 6×8
2 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 4 1 1 1 1 1 1 1 1 5 1 1 1 1 1 1 1 1 6 1 1 1 1 1 1 1 1 7 1 1

Sign in to comment.

Accepted Answer

DGM
DGM on 17 Oct 2021
Edited: DGM on 17 Oct 2021
Actually, using a loop was pretty dang simple, now that I tried it.
A = randi(9,5,10);
b = 11:20;
diagonalthing(A,b)
ans = 5×10
11 1 1 1 1 1 1 1 1 1 1 12 1 1 1 1 1 1 1 1 1 1 13 1 1 1 1 1 1 1 1 1 1 14 1 1 1 1 1 1 1 1 1 1 15 1 1 1 1 1
diagonalthing(A.',b(1:3))
ans = 10×5
11 1 1 1 1 1 12 1 1 1 1 1 13 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
function C = diagonalthing(A,b)
% synopsis goes here bla bla bla
s = size(A);
mindim = min(s(1),s(2));
C = ones(s);
for k = 1:min(mindim,numel(b))
C(k,k) = b(k);
end
end
  2 Comments
Humza Khan
Humza Khan on 17 Oct 2021
I have one more question:
How would you do this operation without using min/numel functions?
Although, I understand what they are doing, i cannot use them in the code since we havent covered them in class.
Thanks!
DGM
DGM on 17 Oct 2021
They could be avoided, but without resorting to other potentially more complex or forbidden functions, things get kind of unreasonably ridiculous.
Depending on what's forbidden, you might be able to get away with:
x = 1:10;
% numel() can be avoided by using size
n = size(x(:),1)
n = 10
% min() can be avoided using sort
mn = sort(x(:),'ascend');
mn = mn(1)
mn = 1
I kind of doubt sort() is allowed if min() isn't allowed. You could always use a loop i guess.
% or you could use a loop
mn = x(1);
for k = 1:numel(x) % have to replace numel again
if x(k) < mn
mn = x(k);
end
end
mn
mn = 1
Of course, it would be desirable to write this as a function so that you don't have to repeat code -- but I imagine you aren't allowed to do that either. That's the way these garbage assignments seem to go.
I don't know which aspect ot these assignments bothers me more, instructing students on how to write the least efficient code possible, or prohibiting students from acting on initiative and learning beyond the lesson.

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!