hi i want to calculate nth power for each value of s from 1 to 50 and store the result for each value of s in za varaible

조회 수: 1 (최근 30일)
f=imread('lena1.jpg');
x=rgb2gray(f);
for s=1:50
za=x.^(s);
end
  댓글 수: 1
Guillaume
Guillaume 2018년 7월 25일
편집: Guillaume 2018년 7월 25일
Oh! I see the question was edited. s starts as an image, then becomes an iterator. That's confusing! There are 26 letters in the alphabet, you don't have to limit yourself to just 2. Even better, use words for variable names as this makes the intent of the code clearer.
What is the desired output? What size should za be?

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

답변 (1개)

Guillaume
Guillaume 2018년 7월 25일
Sounds like homework to me.
Do you have to use a loop? The normal matlab way is simply:
za = (1:50).^2;
If you do have to use a loop, index your za.
Note that there is a big difference between 2.^x and x.^2. The power operation is not commutative.
  댓글 수: 3
Guillaume
Guillaume 2018년 7월 25일
In your question, you wrote "calculate square" which .^2.
What does (image) mean? Perhaps, you should show a numerical example of input and corresponding desired output.
Guillaume
Guillaume 2018년 7월 25일
편집: Guillaume 2018년 7월 25일
Ok, so what do you want as output? Should za be a 3D array of size height(image) x width(image) x 50?
Perhaps, this is what you want:
img = im2double(rgb2gray(imread('lena1.jpg'));
s = 1:50;
za = img .^ permute(s, [1 3 2]); %MxNx50 array

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by