フィルターのクリア

How to use Loop without For or While

2 ビュー (過去 30 日間)
Ardalan Norouzi
Ardalan Norouzi 2015 年 11 月 29 日
コメント済み: Jan 2015 年 12 月 6 日
Hello everyone Consider this code:
n=10;
m=5;
A=zeros(n,m);
A(1,:)=[1];
for i=1:n-1
A(i+1,:)=3*A(i,:)
end
answer is :
A =
1 1 1 1 1
3 3 3 3 3
9 9 9 9 9
27 27 27 27 27
81 81 81 81 81
243 243 243 243 243
729 729 729 729 729
2187 2187 2187 2187 2187
6561 6561 6561 6561 6561
19683 19683 19683 19683 19683
How can I calculate the A without using For (or while) , like this code:
n=10;
m=5;
A=zeros(n,m);
A(1,:)=[1];
A(2:end,:)=3*A(1:end-1,:)
unfortunately this code appears this:
A =
1 1 1 1 1
3 3 3 3 3
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Thanks every one
  2 件のコメント
James Tursa
James Tursa 2015 年 11 月 29 日
編集済み: James Tursa 2015 年 11 月 29 日
Is this homework? What's wrong with using your loop?
Ardalan Norouzi
Ardalan Norouzi 2015 年 11 月 29 日
編集済み: Ardalan Norouzi 2015 年 11 月 29 日
yes it's question of my teacher for homework please help me

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

回答 (3 件)

Walter Roberson
Walter Roberson 2015 年 11 月 29 日
See bsxfun
  3 件のコメント
Jan
Jan 2015 年 11 月 29 日
編集済み: Jan 2015 年 11 月 29 日
@Ardalan Norouzi: It is your homework. If the complete solution is posted in the forum, you have to cheat when you submit it. you can find the solution by your own, when you read the documentation of bsxfun.
Ardalan Norouzi
Ardalan Norouzi 2015 年 11 月 29 日
編集済み: Ardalan Norouzi 2015 年 11 月 29 日
I'm trying to write the code but I can't complete it, Please help me

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


Jan
Jan 2015 年 11 月 29 日
2 hints:
3 .^ (0:9)
% [1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683]
[1;2;3] * [4,5,6]
% [4, 5, 6;
% 8, 10, 12;
% 12, 15, 18]
  4 件のコメント
Ardalan Norouzi
Ardalan Norouzi 2015 年 11 月 29 日
編集済み: Ardalan Norouzi 2015 年 11 月 29 日
I would like to create an array of elements which created with their last elements. for example A is an array with 1 row and 5 column , first column is 1 and second column is 3*A(first) and third column is 3*A(second) and ... The whole of this procedure without using "For" or "While"
Jan
Jan 2015 年 12 月 6 日
So what's wrong with the hints I gave?! I hesitate to post the complete solution, because otherwise you cannot submit this as a your homework without cheating. But the shown methods allow to solve the problem easily. If you do not only claim, that they do not help you but post, what you have tried and why you think, that this does not help, I had a chance to give further hints.
3 .^ (0:9).'
Now this must by multiplied by a vectors of ones to obtain the shown matrix.

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


Ardalan Norouzi
Ardalan Norouzi 2015 年 11 月 30 日
Please somebody help me , I need this code , you're my last chance.
  1 件のコメント
Jan
Jan 2015 年 12 月 6 日
See my comment.

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

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by