フィルターのクリア

For Loop Moving Through Array

192 ビュー (過去 30 日間)
HoboHarry
HoboHarry 2018 年 8 月 30 日
編集済み: Stephen23 2018 年 8 月 30 日
Trying to write a for loop that moves through an array with 2 columns and 15 rows. The code i've written just provides the same results 15 times for the 1st cells of the array. i'm guessing i need to nest it somehow but i have no idea, a few clues on where i'm wrong would be great
X = Var1;
Y = Var2;
Z = 29;
TableCreate = table(X,Y);
Array = table2array(TableCreate);
for N = 1:length(Array)
A = (Array(1,1)*Z)/Array(1,2);
disp (A)
end
  1 件のコメント
Stephen23
Stephen23 2018 年 8 月 30 日
編集済み: Stephen23 2018 年 8 月 30 日
"i'm guessing i need to nest it somehow but i have no idea, a few clues on where i'm wrong would be great"
Inside the loop you need to use indexing into A:
Even better, don't use any loop at all:

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

採用された回答

Sven
Sven 2018 年 8 月 30 日
The problem is that you are not using the index N.
A = (Array(1,1)*Z)/Array(1,2);
This line doesn't change, so you will get the same output 15 times. A nested loop would look like this:
[row,col] = size(B);
for N = 1:col
for M = 1:row
A = (B(M,N)*Z)/B(1,2);
disp (A)
end
end
The first loop will start at column 1, then the second loop goes through all rows. After that it repeats with column 2 and so on if you have more columns. Depending on how you want to manipulate the entries you have to use both indexes accordingly.

その他の回答 (1 件)

Dennis
Dennis 2018 年 8 月 30 日
編集済み: Dennis 2018 年 8 月 30 日
You overwrite A in every iteration and you are calculating the same value over and over again (not using N anywhere).
I am not sure why you create a table and turn it into an array afterwards.
MWE:
x=randi(3,3);
y=rand(3);
array=[x, y];
A=zeros(size(array,2),1);
for N=1:size(array,2)
A(N)=array(1,N)+array(2,N); %use N to access array index, i have no clue what you try to calculate so my example is random
end
disp(A)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by