How can i make a matrix with variables from workspace.

조회 수: 4 (최근 30일)
Márcis Pinups
Márcis Pinups 2021년 10월 29일
답변: Kshitij Chhabra 2021년 11월 3일
I have made a matrix and saved the matrix lines seeratly, as for example: line1, line 2, line 3
How can I write a for loop for creating a matrix, where the programm asks which lines i want o include in my new matrix.
%Those lines are saved as a vector
line1=simulink_matrix(1,:);
line2=simulink_matrix(2,:);
line3=simulink_matrix(3,:);
  댓글 수: 1
Stephen23
Stephen23 2021년 10월 29일
The MATLAB approach is to use an index:
idx = [2,4,11]; % indices of the rows you want
out = simulink_matrix(idx,:);
Your current approach (with lots of numbered variables) will make this task slow, complex, and very inefficient.

댓글을 달려면 로그인하십시오.

답변 (1개)

Kshitij Chhabra
Kshitij Chhabra 2021년 11월 3일
Hi Marcais,
As Stephen mentioned in one of the comments, creation of numbered variables would be an in-efficient approach. However as you mentioned that, you want the program to ask the lines which you want to include in the output, I can recommend using the "input" keyword inside a for loop and then perform the related options accordingly. You can check the follwing code for reference
You can check the documentation for input here.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by