Info

この質問は閉じられています。 編集または回答するには再度開いてください。

I need help with defining while loops?

2 ビュー (過去 30 日間)
Najib Ahmadi
Najib Ahmadi 2015 年 11 月 13 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hello. I want write a code which can performe to generate matrix automaticlly. In the last while loop in my code I want to define that the 3:rd column and 2:nd row is =5 and the 2:nd column and 2:nd row is = 7 and the rest of the row tow is = 0 when we do not have any more number. Plz help me to solve this issue.
My code
% -------------------------------------
a=386;
b=75;
nrows=2;
stra= num2str(a); %#########convert (a) to string format
strb=num2str(b); %#########convert (b) to string format
sizea= length(stra);
sizeb= length(strb);
% M=zeros(sizea, sizeb);
if sizea > sizeb
ncols = sizea
M= zeros (nrows, ncols)
while sizea >= 1
M(1, sizea) = str2num(stra(1, sizea)) %(stra(stra(1), size(a)))
sizea=sizea-1
end
else
ncols=sizeb
M=zeros (nrows, ncols)
end
while sizeb >= 1
M(2, sizeb) = str2num(strb(1,sizeb))
sizeb=sizeb-1
sizea=sizea-1
end
  2 件のコメント
Stephen23
Stephen23 2015 年 11 月 13 日
@Najib Ahmadi: most of the volunteers here already know how to write efficient MATLAB code. What we don't know is exactly what you are trying to do. We can fix your code, or write something to help you, but you need to explain exactly what you need.
Please post input and output data that we can test code with, and an detailed explanation. Posting broken code does not help us to understand what you are trying to achieve.
Thorsten
Thorsten 2015 年 11 月 13 日
What is the expected output in your example with a=386 and b=75?

回答 (2 件)

Eng. Fredius Magige
Eng. Fredius Magige 2015 年 11 月 13 日
Your increment is missing which support to allow application of while. Both if and else are inner within wile loop as well as to end by end.
  1 件のコメント
Najib Ahmadi
Najib Ahmadi 2015 年 11 月 13 日
Is it possible for you write the code. Because in my case it did not work! appriciate for ur help

Thorsten
Thorsten 2015 年 11 月 13 日
編集済み: Thorsten 2015 年 11 月 13 日
Do you want to put the digits of a "a" and "b" into the elements of a matrix? The you can use this code:
M = num2str([a; b]) - '0';
M(M==' ' - '0') = 0
M =
3 8 6
0 7 5

Community Treasure Hunt

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

Start Hunting!

Translated by