MATLAB Answers

how to do subtraction inside Structure

51 ビュー (過去 30 日間)
tabw
tabw 2014 年 8 月 13 日
コメント済み: Azzi Abdelmalek 2014 年 8 月 14 日
1xN Structure call Structure.result Structure(1).result=1x5 Array ,[ 1 2 3 4 5]
Structure(2).result=1x7 Array,[1 2 3 4 5 6 7]
How to do Structure(2).result -Structure(1).result
I mean Every element inside Structure(2) - every structure inside Structure(1)
I tried
Structure(2).result(1:7) - Structure(1).result(1:5)
But it won't work.
Is it a must to write a loop? like
for i=1:5
Structure(2).result(1:7) - Structure(1).result(i)
end
Actually, what I want to do is that
1xN Structure call Structure.result
for i= 1:N
Structure(i).result=1x5 Array ,[ 1 2 3 4 5]
I want to find every array contained inside the structure - every array there EG (Structure(1).result=1x5 Array ,[ 1 2 3 4 5]-
Structure(i).result=1x7 Array,[1 2 3 4 5 6 7])
and find the any number == 10;
if number =10
combine Struct(1)and Struct (2)into New structure
and use the New struct to do the substraction

  0 件のコメント

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

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 8 月 13 日
編集済み: Azzi Abdelmalek 2014 年 8 月 13 日
Edit
a=struct('result',{1:5,1:7})
b=struct('result',num2cell(bsxfun(@minus,a(1).result',a(2).result)))

  1 件のコメント

tabw
tabw 2014 年 8 月 13 日
Thank you so much.You are really good at matlab.

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


Pratik Bajaria
Pratik Bajaria 2014 年 8 月 13 日
Well Technically its not possible. You are trying to subtract 1x5 from 1x7 array, which is violation of dimensionality. As a workaround, what you can do is the idea of padding dummy numbers like zeros to 1x5 and make it a 1x7 array/vector and then probably subtraction should work. I hope this helps.
Please let me know in case of further doubts.
Regards, Pratik

  3 件のコメント

tabw
tabw 2014 年 8 月 13 日
How to pad with zeros?
Pratik Bajaria
Pratik Bajaria 2014 年 8 月 14 日
Although, solution posted above seems working (by Azzi Abdelmalek), which is Great. Here's what i would have done to the most basic level.
1. Get the size of biggest array from the given structure. 2. Pad the deficit zeros using 'zeros(x,y)' command in MATLAB. 3. Make them all in Unison w.r.t. dimensions. 4. Subtract.
Although, this idea seems more like a workaround, the one posted by Azzi Abdelmalek seems Great!
Anyways, Result matters. Even i got to learn something new.
Regards, Pratik.
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 8 月 14 日
Patrick, padding with zeros is not what Tabw is asking for, and our solutions are completely different.

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by