operations on two different matrices

I need to replace the first row from a matrix A(m.m) by first row from another matrix B(n.n) with different size?

 Risposta accettata

D0 j=1,8
A(1, j)=D(2,j+1)
end do
Acol = size(A,2);
A(1,:) = D(2,2:Acol+1);
vec = DD1(1,:);
A1(idx,:) = repmat(vec,numel(idx),1);
Acol = size(A,2);
vec = DD1(1,1:Acol);
A1(idx,:) = repmat(vec, numel(idx), 1);

Più risposte (1)

the cyclist
the cyclist il 14 Set 2021
Modificato: the cyclist il 15 Set 2021
Suppose ...
m = 3;
n = 4;
A = magic(m)
A = 3×3
8 1 6 3 5 7 4 9 2
B = magic(n)
B = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
The first row of A is a 1x3 row vector, and the first row of B is a 1x4 row vector. What do you want the output to be? Also, what if m had been greater than n?

6 Commenti

well, I want to replace the the first row of A by the elements 2 3 13 from the first row of B.
In another case, if A: n x n and B: m x m and n +2 = m want to replace A(1, :) by B(1: 2 to n-1).
Sorry, I still don't understand. Let's take a few specific cases. (The vectors represent the first row.) What should the output be in these cases?
Case 1
A = [8 1 6];
B = [16 2 3 13];
newA = [2 3 13]; % This is what you just said, so confident here.
Is the general rule that you take from the right-hand end?
Case 2
A = [16 2 3 13];
B = [8 1 6];
newA = ?; % What should it be?
Case 3
A = [30 39 48 1 10 19 28];
B = [8 1 6];
newA = ?; % What should it be?
Case 4
A = [8 1 6];
B = [30 39 48 1 10 19 28];
newA = ?; % What should it be?
I'm just trying to understand the general rule, and make sure it covers all possible cases.
First of all, I would like to thank you for your interesting; the next case may be represent my problem:
If we have A(8,8) and D(10,10) and we can write the following Do-Loop in Fortran:
D0 j=1,8
A(1, j)=D(2,j+1)
end do
is there a simple method to transform into Matlab?
Soryy, I have tried for the case of A1 and DD1 are of equal size but it did not work; I think may be becaue of A1 is complex while DD1 is real?:
vec = DD1(1,:);
A1(idx,:) = repmat(vec,numel(idx),1);
Sorry, but your last two comments are just confusing to me.
You did not respond to my questions. What are the results for Case 1, Case 2, etc?
I don't know Fortran anymore, so cannot interpret that.
Why are you suddenly introducing new variables (D, DD1, idx)? Your original question seemed very simple, about filling in the first row of a matrix A, from the first row of a matrix B. It seems to be getting more and more complicated with every comment.
The first case is the nearest one to my problem.The symbols D, DD1, ...are matrices, but I'm sorry about any confusion. Would you please help me for the first case?

Accedi per commentare.

Prodotti

Release

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by