Turning a 1x3001 array into a 14x3001 array to make two things the same size and perform math on them...

조회 수: 1 (최근 30일)
Hello,
Super new with MatLab so apologies for ignorance, it's for a course on Signals.
I am trying to compare the ground truth signal, the single frequency, which has 3001 samples (it is a 1x3001 row vector) to a 14x3001 array. I want to add the abs value of the 1x3001 vector to the abs value of the each of the 14 rows of the array.
So I would like to repeat the 1x3001 row 14 times to add the abs value of that to the 14x3001 array.
Stuck on how to get the 1x3001 to repeat 14 times and turn into its own 14x3001 array to work with the second array...
Element by element and then graph it later, with maybe a stem...
Thanks for any and all help...

채택된 답변

Voss
Voss 2024년 9월 7일
편집: Voss 2024년 9월 7일
You don't have to explicitly repeat the 1x3001 row vector 14 times in order to add it to the 14x3001 matrix.
Here's an example of adding a 1x5 row vector to a 3x5 matrix to illustrate the syntax:
v = 1:5
v = 1x5
1 2 3 4 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
m = rand(3,5)
m = 3x5
0.7662 0.0984 0.5704 0.1726 0.2253 0.2508 0.5595 0.7907 0.4376 0.4649 0.8281 0.1733 0.7462 0.9606 0.3230
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
result = v+m
result = 3x5
1.7662 2.0984 3.5704 4.1726 5.2253 1.2508 2.5595 3.7907 4.4376 5.4649 1.8281 2.1733 3.7462 4.9606 5.3230
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
However, if you need to create a matrix consisting of multiple copies of a row vector for some other purpose, here's one way to do that:
vv = repmat(v,3,1)
vv = 3x5
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
And another way:
vv = v(ones(3,1),:)
vv = 3x5
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
And adding that matrix to the other one works as expected
result = vv+m
result = 3x5
1.7662 2.0984 3.5704 4.1726 5.2253 1.2508 2.5595 3.7907 4.4376 5.4649 1.8281 2.1733 3.7462 4.9606 5.3230
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by