Multiplication of vectors in for loops

2 ビュー (過去 30 日間)
Jade T
Jade T 2023 年 2 月 15 日
編集済み: Voss 2023 年 3 月 1 日
Hi all,
I am wondering how to make this for loop work. Each radius value results in a vector product containing 3 numbers. I wanted those three numbers to occupy each row ( so that each row is a new vector from each radius multiplication ).
radius = [3,3] % contains 9 different values
Tdims= zeros(9,3)
for i = 1: length(radius)
for j= radius
Tdims(j,i)= sin (anglesRad) .* radius (j) + ycenter - 100 % each vector product is its own row
end
end

採用された回答

Voss
Voss 2023 年 2 月 15 日
Possibly this:
radius = rand(3,3); % contains 9 different values
ycenter = 0;
anglesRad = [0 1 2]; % I guess this is a 1-by-3 vector based on your description
nr = numel(radius);
na = numel(anglesRad);
Tdims = zeros(nr,na);
for i = 1:nr
Tdims(i,:) = sin(anglesRad) .* radius(i) + ycenter - 100; % each vector product is its own row
end
disp(Tdims);
-100.0000 -99.3458 -99.2931 -100.0000 -99.8789 -99.8691 -100.0000 -99.8605 -99.8493 -100.0000 -99.3679 -99.3170 -100.0000 -99.9411 -99.9364 -100.0000 -99.2905 -99.2334 -100.0000 -99.9297 -99.9240 -100.0000 -99.3072 -99.2513 -100.0000 -99.3274 -99.2732
  2 件のコメント
Jade T
Jade T 2023 年 2 月 15 日
Excellent, I actually can understand what you did here, and you caught that the center needed to be specified outside the loop. Much appreciated
Voss
Voss 2023 年 2 月 15 日
編集済み: Voss 2023 年 3 月 1 日
You're welcome!

サインインしてコメントする。

その他の回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 2 月 15 日
編集済み: Sulaymon Eshkabilov 2023 年 2 月 15 日
If understood your question correctly, this is how you can get it done:
radius = randi(10, 3, 3); % contains 9 different values
ycenter = 1;
anglesRad = deg2rad([30, 60 90]);
Tdims= zeros(size(radius));
for ii = 1:size(radius,1)
for jj= 1:size(radius,2)
Tdims(ii,jj)= sin(anglesRad(ii))* radius (ii,jj) + ycenter - 100; % each vector product is its own row
end
end
Tdims
Tdims = 3×3
-97.5000 -96.0000 -97.5000 -90.3397 -98.1340 -96.4019 -90.0000 -90.0000 -91.0000

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by