Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

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 vue (au cours des 30 derniers jours)
Mahmoud Ahmed
Mahmoud Ahmed le 8 Mai 2017
Clôturé : MATLAB Answer Bot le 20 Août 2021
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 commentaires
KSSV
KSSV le 8 Mai 2017
It is confusing...an example would help.
Mahmoud Ahmed
Mahmoud Ahmed le 8 Mai 2017
Modifié(e) : Mahmoud Ahmed le 8 Mai 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

Réponses (2)

KSSV
KSSV le 8 Mai 2017
A = [1:4 ; 5:8 ; 9:12 ; 13:16] ;
B = zeros(4,5) ;
for i = 1:4
B(:,i) = A(:,i)
end
  4 commentaires
Mahmoud Ahmed
Mahmoud Ahmed le 8 Mai 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
KSSV
KSSV le 8 Mai 2017
A = [1:4 ; 5:8 ; 9:12 ; 13:16] ;
B = zeros(4,5) ;
for i = 1:4
B(i,1) = A(i,1)
end

Guillaume
Guillaume le 8 Mai 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

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by