how to create a for loop for 3 variables

%how can I reiteratively create a foor loop for the following:
Number1 = Point(1:15*7);
Number2 = Point(1+15*7 : 2*15*7);
Number3 = Point(1+2*(15*7) : 3*(15*7);

 採用された回答

the cyclist
the cyclist 2022 年 1 月 4 日
編集済み: the cyclist 2022 年 1 月 4 日

0 投票

% Make up a random input. (You don't need this step)
Point = rand(1,3*15*7);
% Define these parameters for convenience
NR = 3;
NC = 15*7;
% Initialize the output array
Number = zeros(NR,NC);
% Fill the array
for n = 1:NR
Number(n,:) = Point((n-1)*NC+1:n*NC);
end
Each variable you have called Number1, etc, is a row of Number. It is better to not use dynamically named variables.

8 件のコメント

the cyclist
the cyclist 2022 年 1 月 4 日
Note that one can also use the reshape function to do a similar manipulation, but it wasn't clear to me that this is really what you intended.
Michael Angeles
Michael Angeles 2022 年 1 月 4 日
Thank you, but how can I save the array for each variable?
the cyclist
the cyclist 2022 年 1 月 4 日
You should not do this. Read why here.
Anywhere that you would have used Number1, use Number(1,:) instead, and it should work.
Michael Angeles
Michael Angeles 2022 年 1 月 5 日
I needed each "Number 1-3" as a separate variable in the workspace. Each one will be used by another part of the code.
the cyclist
the cyclist 2022 年 1 月 5 日
Number1 = Number(1,:);
Stephen23
Stephen23 2022 年 1 月 5 日
"I needed each "Number 1-3" as a separate variable in the workspace."
I doubt that you "needed" that.
"Each one will be used by another part of the code."
Using indexing is the easist way of achieving that.
the cyclist
the cyclist 2022 年 1 月 5 日
100% agree with @Stephen, even though I finally caved in a told you how to do what you asked for.
I strongly suggest you read the info I provided on why it's a bad idea to name variables Number1, Number2, etc.
Michael Angeles
Michael Angeles 2022 年 1 月 5 日
the cyclist - that worked....thanks!!

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

その他の回答 (1 件)

DGM
DGM 2022 年 1 月 4 日
編集済み: DGM 2022 年 1 月 5 日

0 投票

This probably isn't exactly what you want, but I'm not sure what you're working with, and the approach can probably be improved. Consider this a suggestion more than a complete answer.
A few points:
  • I don't know what Point is. I'm going to assume it's a vector and that it's integer-divisible by 105
  • Embedding indexing information in variable names is counterproductive. Avoid that in favor of using array indexing or something
  • Use meaningful variable names. Calling a numeric variable "Number" conveys no information.
% i assume that numel(Point) is integer-divisible by 105
Point = 1:315;
% now each row of Point is what you would have called NumberX
Point = reshape(Point,105,[]).';
% say those are x,y,z components of a point list
plot3(Point(1,:),Point(2,:),Point(3,:)); grid on
If Point is not necessarily integer-divisible by the assumed row length and any excess is to be ignored, you can simply truncate the length of the vector before reshaping it.
rl = 105; % row length
Point = 1:320; % not integer-divisible by row length
Point = Point(1:rl*floor(numel(Point)/rl)); % truncate
Again, it's hard to know what you're after. I'm assuming you're intending to generalize for more than 3 rows.

カテゴリ

ヘルプ センター および 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