Create a vector with for loop with mathematical operations

w=[5 4 3 2]
Use this vector in a mathematical expression to create the following vectors:
My code snippet is below, code for that question. When I run it, I can't get expected result. I just want to create a vector as noted above.How can I solve that question?
w=[5 4 3 2]
for i= w
a3=[1/(i+i)]
b3 =[i^i]
c3 =[i/sqrt(i)]
d3 =[(i^2)/(i^i)]
end

 採用された回答

KSSV
KSSV 2020 年 5 月 10 日

0 投票

Read about ., element by element operations.
w = [5 4 3 2] ;
a = 1./(w+w) ;
b = w.^w ;
c = w./sqrt(w) ;
d = w.^2./w.^w ;

その他の回答 (1 件)

Toder
Toder 2020 年 5 月 10 日

1 投票

To fix your current code, you need to use indices in the for loop.
w=[5 4 3 2]
for i = w
a3(6-i)=[1/(i+i)]
b3(6-i) =[i^i]
c3(6-i) =[i/sqrt(i)]
d3(6-i) =[(i^2)/(i^i)]
end
This assigns a3(1) in the first loop, a3(2) in the second, etc. Your code reassigns the scalar a3 each loop. The indices I use are somewhat strange because we're looping over w, which is fine, it just makes indices harder to keep track of. You could do it differently by using a counter:
w=[5 4 3 2]
j = 1
for i = w
a3(j)=[1/(i+i)]
b3(j) =[i^i]
c3(j) =[i/sqrt(i)]
d3(j) =[(i^2)/(i^i)]
j = j+1
end
Or you could have your for loop count from 1 to 4:
w=[5 4 3 2]
for i = 1:4
a3(i)=[1/(w(i)+w(i))]
b3(i) =[w(i)^w(i)]
c3(i) =[w(i)/sqrt(w(i))]
d3(i) =[(w(i)^2)/(w(i)^w(i))]
end
All this being said, I recommend "vectorizing" your code where possible. Dot operators perform arithmetic element-by-element, see the documentation for dot multiplication here. For instance, the whole a3 vector can be defined this way:
a3 = 1./(2*w)

5 件のコメント

Hasan Berke Bankoglu
Hasan Berke Bankoglu 2020 年 5 月 10 日
Thank you very much for your detailed answers. The other answerer gave the codes that produced the output exactly as in the question. Thank you again. There is a problem with giving a3 multiple times in the for loop.
Toder
Toder 2020 年 5 月 11 日
What's the problem? If it's that each vector is printing with each iteration, you can put a semicolon at the end of each assignment line in the foor loop to suppress the output.
Hasan Berke Bankoglu
Hasan Berke Bankoglu 2020 年 5 月 12 日
編集済み: Hasan Berke Bankoglu 2020 年 5 月 12 日
When I run those codes or my code, I took that output below; but, I just need one output for a3,b3,c3,d3 as submitted photo. I think it's about for loop's working style. It gives outputs the number of elements of w.
w = 1×4
5 4 3 2
a3 = 1×4
0.1000 0.1250 0.1667 0.2500
b3 = 1×4
3125 256 27 4
c3 = 1×4
2.2361 2.0000 1.7321 1.4142
d3 = 1×4
0.0080 0.0625 0.3333 1.0000
a3 = 1×4
0.1000 0.1250 0.1667 0.2500
b3 = 1×4
3125 256 27 4
c3 = 1×4
2.2361 2.0000 1.7321 1.4142
d3 = 1×4
0.0080 0.0625 0.3333 1.0000
a3 = 1×4
0.1000 0.1250 0.1667 0.2500
b3 = 1×4
3125 256 27 4
c3 = 1×4
2.2361 2.0000 1.7321 1.4142
d3 = 1×4
0.0080 0.0625 0.3333 1.0000
a3 = 1×4
0.1000 0.1250 0.1667 0.2500
b3 = 1×4
3125 256 27 4
c3 = 1×4
2.2361 2.0000 1.7321 1.4142
d3 = 1×4
0.0080 0.0625 0.3333 1.0000
David Banahene
David Banahene 2022 年 1 月 23 日
I have a vector a=[1:5] and would want to know how to plot (a-3)^3.
Stephen23
Stephen23 2022 年 1 月 23 日
a = 1:5;
b = (a-3).^3;
plot(a,b)

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by