Info

This question is closed. Reopen it to edit or answer.

I have 2 matrices with size (4,4) and (4,5) respectively. I want the element (2,1) in the second matrix to try all values in the first column in the first matrix and at each value, produce new matrix

1 view (last 30 days)
I have 2 matrices with size (4,4) and (4,5) respectively. I want the element located at (2,1) in the second matrix to take all the values in the first column in the first matrix. How can I do this?
  4 Comments
Mahmoud Ahmed
Mahmoud Ahmed on 8 May 2017
Edited: Mahmoud Ahmed on 8 May 2017
A = [1:4 ; 5:8 ; 9:12 ; 13:16] B = zeros(4,5) B(2,1) = 0 now. I want B(2,1) to try all the values from the 1st column in A matrix B(2,1) = A(:,1) B(2,1) equals at the 1st time 1 at 2nd time 4 at 3rd time 9 at 4th time 13 and at each time I want to see the new B matrix

Answers (2)

KSSV
KSSV on 8 May 2017
A = [1:4 ; 5:8 ; 9:12 ; 13:16] ;
B = zeros(4,5) ;
for i = 1:4
B(:,i) = A(:,i)
end
  4 Comments
Mahmoud Ahmed
Mahmoud Ahmed on 8 May 2017
with this code, the output is B =
1 0 0 0 0
5 0 0 0 0
9 0 0 0 0
13 0 0 0 0
and I want B(2,1) only to be assigned with each value from the 1st column of A at different times

Guillaume
Guillaume on 8 May 2017
Probably the easiest is to replicate your B in the 3rd dimension and copy the column of A along that dimension.
A = [1:4 ; 5:8 ; 9:12 ; 13:16]
B = rand(4, 5);
newB = repmat(B, [1, 1, size(A, 1)]); %replicate B in the 3rd dimensions as many times as there are rows in A
newB(2, 1, :) = A(:, 1); %and copy 1st column of A, overwrite whatever was in B(2, 1)
You can then iterate over the pages (3rd dimension) of that newB to do whatever it is you want to do.
for page = 1 :size(newB, 3)
%do something with B(:, :, page)
end

This question is closed.

Community Treasure Hunt

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

Start Hunting!