shift the first 5 characters of str to the end using loops

1 回表示 (過去 30 日間)
taki shi
taki shi 2019 年 10 月 15 日
コメント済み: Walter Roberson 2019 年 10 月 15 日
so for ex i have
str = i love matlab
circshift(str,-5)
but how do i use LOOPS

回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 10 月 15 日
You need to create an output vector the same size as the input vector. Then copy from location 6 of input to location 1 of output, location 7 of input to location 2 of output, location 8 of input to location 3 of output, and so on, until you reach the end of the input. Then copy location 1 of input to the 5th last output, location 2 of input to the 4th last output, and so on, until 5th location of input to the last output.
There is a way to do this all in one single loop with no if statements using a bit of clever mathematics. There is a way to do all this in one single loop with an if/else statement with no clever mathematics. There is a way to do it in two loops with no if statement.
  7 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 10 月 15 日
編集済み: KALYAN ACHARJYA 2019 年 10 月 15 日
"this is no help im reporting"
exuse you im not asking for your opinion i know how shit is done dont come commenting on here
Hello Taki Shi,
Do you know, who is Walter Roberson? Ask to Mathworks, they will provide you detail. We all in this forumn expect polite comments. Walter gave you sufficient hints to solve it by yourself. Still if you need more detail with comple code, you can politely ask for it. But please don't demand for it, all answers provider in this forumn are doing volueering services (Nobody pays to anyone)
Walter Roberson
Walter Roberson 2019 年 10 月 15 日
for loops are documented at https://www.mathworks.com/help/matlab/ref/for.html . for is probably the single most common name in computer languages for loops of fixed length, being used in the fortran class of languages; in the C/C++ class of languages; in Java class of languages; in the perl class of languages; in the sh class of languages; in BASIC; in Excel; it even occurs in LISP. It is true, though, that a different verb was used in COBOL.
The other thing you need to know is indexing; https://www.mathworks.com/help/matlab/math/array-indexing.html
These are both discussed in the introductory documentation on MATLAB and in the free online courses; https://www.mathworks.com/matlabcentral/answers/8026-best-way-s-to-master-matlab
If you feel that you are missing an important concept in looping, you can ask a clarifying question.
In my over 40 years of helping people learn to program, I have found that the biggest stumbling block in loops for beginning programmers is in learning to see how to break down a problem into repetitive steps that it makes sense to loop over; the second largest stumbling block I find is in explaining arrays and indexing variables. I find that people who have learned about indexing can sometimes continue to have difficulty seeing how to break down a particular problem into repetitive steps that indexing could be applied to.

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

カテゴリ

Help Center および 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