Hi, I would like to get all nine possible values of these two sets of s and c vectors. When I try to use for loop, I only get one value. Can anyone please help in making this loop work?
Thank you.
close all; clc; clear all;
s = [1.2535 1.2535 1.2535
c = [0.0 0.5 1.0]
for i=1:100%:5;
for j=1:20%:5;
Formula = s(i)/(sqrt(c(j)));
end
end

 採用された回答

Cedric
Cedric 2017 年 10 月 16 日
編集済み: Cedric 2017 年 10 月 16 日

1 投票

s = [1.2535 1.2535 1.2535]
c = [0.0 0.5 1.0]
Formula = zeros( numel(s), numel(c) ) ;
for i=1:3
for j=1:3
Formula(i,j) = s(i)/sqrt(c(j));
end
end
but if you have a recent version of MATLAB, it is simpler to do it using an expansion:
Formula = s.' ./ sqrt(c) ;

4 件のコメント

Ismail Qeshta
Ismail Qeshta 2017 年 10 月 16 日
編集済み: Ismail Qeshta 2017 年 10 月 16 日
Thanks Cedric for the suggestion. How about if I have different size vectors, like the one shown below? In this case I will have 18 possible values out of the formula. Can you please suggest to me how to build the loop? Thanks
s= [1.2535 1.2535 1.2535];
c=[0.0 0.5 1.0 1.5 1.4 1.5]
Cedric
Cedric 2017 年 10 月 16 日
I should have done it in my initial answer directly. Here it is:
s = [1.2535 1.2535 1.2535] ;
c = [0.0 0.5 1.0 1.5 1.4 1.5] ;
ns = numel(s) ;
nc = numel(c) ;
Formula = zeros(ns, nc) ; % Prealloc. memory.
for i = 1 : ns
for j = 1 : nc
Formula(i,j) = s(i)/sqrt(c(j));
end
end
Ismail Qeshta
Ismail Qeshta 2017 年 10 月 16 日
Thank you very much Cedric
Cedric
Cedric 2017 年 10 月 16 日
My pleasure!

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

その他の回答 (0 件)

カテゴリ

質問済み:

2017 年 10 月 16 日

コメント済み:

2017 年 10 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by